2023-06-20 20:04:13 -05:00
|
|
|
|
;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is free software: you can redistribute it and/or
|
|
|
|
|
;;;; modify it under the terms of the GNU General Public License as
|
|
|
|
|
;;;; published by the Free Software Foundation, either version 3 of
|
|
|
|
|
;;;; the License, or (at your option) any later version.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;;; GNU General Public License for more details.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2023-06-28 10:15:23 -05:00
|
|
|
|
;;;; FLORA-SEARCH-AURORA.UTIL
|
|
|
|
|
;;;; Useful misc. utilities used in multiple packages.
|
2023-06-20 20:04:13 -05:00
|
|
|
|
;;;; Let's get to it, we're on a deadline!
|
|
|
|
|
|
|
|
|
|
(in-package :flora-search-aurora.util)
|
|
|
|
|
|
2023-07-01 12:50:24 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Linewrapping & its helpers
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
(defun search-all (subseq sequence &key (start 0))
|
|
|
|
|
"Given a SUBSEQ to search for within a SEQUENCE, return every instance of
|
|
|
|
|
SUBSEQ in SEQUENCE."
|
|
|
|
|
(let ((matches '()))
|
|
|
|
|
(loop while (setf start (search subseq sequence :start2 start))
|
|
|
|
|
do (progn (pushnew start matches)
|
|
|
|
|
(incf start)))
|
|
|
|
|
(reverse matches))) ;; So they’re in ascending order!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun closest-below (num number-list)
|
|
|
|
|
"Given a NUMBER-LIST, return a descending list of member numbers below NUM."
|
|
|
|
|
(sort
|
|
|
|
|
(remove-if-not (lambda (a) (and (numberp a) (<= a num))) number-list)
|
|
|
|
|
#'>))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun fit-lines (string width &key (alignment :center))
|
|
|
|
|
"Fit each line of a STING into a specific WIDTH, with ALIGNMENT to a specific
|
|
|
|
|
side (either :CENTER, :LEFT, or :RIGHT)."
|
|
|
|
|
(str:unlines
|
|
|
|
|
(mapcar (lambda (line)
|
|
|
|
|
(str:fit width line :pad-side alignment))
|
|
|
|
|
(str:lines string))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun linewrap-string (string width)
|
|
|
|
|
"Break a STRING into several lines, each one no larger than WIDTH. Uses
|
|
|
|
|
newlines and hypens (to break long words) as necessary."
|
2023-07-02 11:54:16 -05:00
|
|
|
|
(let* ((string (str:replace-all (string #\newline) " " string))
|
|
|
|
|
(spaces (append '(0) (search-all " " string)))
|
|
|
|
|
(index width))
|
2023-07-01 12:50:24 -05:00
|
|
|
|
(loop while (< index (length string))
|
|
|
|
|
do (let ((closest-space (car (closest-below index spaces)))
|
|
|
|
|
(old-index (- index width)))
|
|
|
|
|
(if (or (<= closest-space old-index)
|
|
|
|
|
(> closest-space index))
|
|
|
|
|
;; Break up long words with a hyphen
|
|
|
|
|
(return
|
|
|
|
|
(linewrap-string
|
2023-07-02 11:54:16 -05:00
|
|
|
|
(str:insert "- " (- index 1) string)
|
2023-07-01 12:50:24 -05:00
|
|
|
|
width))
|
|
|
|
|
;; Replace eligible spaces with newlines uwu
|
|
|
|
|
(progn
|
|
|
|
|
(setf (elt string closest-space) #\newline)
|
|
|
|
|
(setf index (+ closest-space width)))))
|
|
|
|
|
finally (return string))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Listic affairs
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-22 19:08:14 -05:00
|
|
|
|
(defun every-other-element (list)
|
|
|
|
|
"Collect every-other-element of a list. E.g., (1 2 3 4) → (1 3)."
|
|
|
|
|
(when list
|
|
|
|
|
(cons (car list)
|
|
|
|
|
(every-other-element (cddr list)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun plist= (a b &key (test #'eql))
|
|
|
|
|
"Return whether or not two property lists are equal, by comparing values of each pair.
|
|
|
|
|
Uses the keys of plist a."
|
|
|
|
|
(let ((keys (every-other-element a)))
|
|
|
|
|
(loop for key in keys
|
|
|
|
|
do (when (not (apply test (list (getf a key)
|
|
|
|
|
(getf b key))))
|
|
|
|
|
(return nil))
|
|
|
|
|
finally (return 't))))
|
|
|
|
|
|
|
|
|
|
|
2023-07-01 12:50:24 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Numeric affairs
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-26 15:43:01 -05:00
|
|
|
|
(defmacro incf-0 (place &optional (Δ 1))
|
|
|
|
|
"INCF the given PLACE, if it’s a number. If not a number, then set it to zero."
|
|
|
|
|
`(if (numberp ,place)
|
|
|
|
|
(incf ,place ,Δ)
|
|
|
|
|
(setf ,place 0)))
|
|
|
|
|
|
|
|
|
|
|
2023-06-20 20:04:13 -05:00
|
|
|
|
(defun at-least (minimum num)
|
|
|
|
|
"This function returns at least every hope and dream you've ever had, and at
|
|
|
|
|
maximum returns your more pitiful of moments."
|
|
|
|
|
(if (< num minimum)
|
|
|
|
|
minimum
|
|
|
|
|
num))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun at-most (maximum num)
|
|
|
|
|
"This function returns at most every hope and dream you've ever had, and at
|
|
|
|
|
minimum returns your more pitiful of moments."
|
|
|
|
|
(if (> num maximum)
|
|
|
|
|
maximum
|
|
|
|
|
num))
|
2023-06-23 11:28:34 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-01 12:50:24 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-07-12 18:29:49 -05:00
|
|
|
|
;;; Linguistic & symbolic affirs
|
2023-07-01 12:50:24 -05:00
|
|
|
|
;;; ———————————————————————————————————
|
2023-07-15 05:04:17 -05:00
|
|
|
|
(defmacro remove-from-alistf (key alist &key (test 'eql))
|
|
|
|
|
"Remove the given item from an associative list destructively."
|
|
|
|
|
`(alexandria:removef
|
|
|
|
|
,alist ,key
|
|
|
|
|
:test (lambda (key item) (,test key (car item)))))
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 18:29:49 -05:00
|
|
|
|
(defun string->symbol (string)
|
|
|
|
|
"Given a STRING with an optionally defined package (e.g., “package:symbol”),
|
|
|
|
|
return it as an appopriate symbol."
|
|
|
|
|
(let* ((split (str:split ":" (string-upcase string)))
|
|
|
|
|
(package (when (eq (length split) 2)
|
|
|
|
|
(car split)))
|
|
|
|
|
(symbol (or (cadr split) (car split))))
|
|
|
|
|
(if package
|
|
|
|
|
(intern symbol package)
|
|
|
|
|
(intern symbol))))
|
|
|
|
|
|
|
|
|
|
|
2023-06-23 12:41:47 -05:00
|
|
|
|
(defun langcode->keysym (str)
|
|
|
|
|
"Given a language’s code (es/cz/it/etc.), return a corresponding key symbol,
|
|
|
|
|
if the language is among the supported. Otherwise, nil."
|
|
|
|
|
(when (stringp str)
|
|
|
|
|
(let ((lang (string-downcase (subseq str 0 2))))
|
|
|
|
|
(cond
|
|
|
|
|
((string-equal lang "eo") :eo)
|
|
|
|
|
((string-equal lang "en") :en)))))
|
|
|
|
|
|
|
|
|
|
|
2023-06-23 11:28:34 -05:00
|
|
|
|
(defun system-language ()
|
|
|
|
|
"Return the system language, if among the supported; otherwise, EN-glish."
|
2023-06-23 12:41:47 -05:00
|
|
|
|
(or (langcode->keysym (uiop:getenv "LANG"))
|
|
|
|
|
:en))
|
2023-06-23 12:54:29 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-14 08:50:02 -05:00
|
|
|
|
(defun getf-lang (plist &key language (fallback-lang :en))
|
|
|
|
|
"With a PLIST containing keys of language-codes, return the property either fitting the
|
2023-06-23 12:54:29 -05:00
|
|
|
|
preferred LANGUAGE, or the backup FALLBACK-LANG (if LANGUAGE’s is NIL)."
|
2023-07-14 08:50:02 -05:00
|
|
|
|
(or (getf plist (or language (ignore-errors *language*) (system-language)))
|
2023-06-23 12:54:29 -05:00
|
|
|
|
(getf plist fallback-lang)))
|
2023-07-14 08:50:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defparameter *language* (…:system-language))
|