utils.lisp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (in-package :stumpwm)
  2. ;; https://stackoverflow.com/a/48122810
  3. (defun filter (predicate x)
  4. (if (consp x) ; if x is a cons, that is a tree:
  5. (let ((ca (car x))
  6. (cd (filter predicate (cdr x)))) ; filter always the cdr
  7. (if (listp ca) ; if the car of x is a list (nil or cons)
  8. (cons (filter predicate ca) cd) ; then filter also the car
  9. (if (funcall predicate ca) (cons ca cd) cd))) ; car is a non-nil atom!
  10. x)) ; if x is a atom (nil or the last cdr of an improper list), return x
  11. (defun file-get-contents (filename)
  12. (with-open-file (stream filename)
  13. (let ((contents (make-string (file-length stream))))
  14. (read-sequence contents stream)
  15. contents)))
  16. (defun join-to-stream (stream list &optional (delimiter #\&))
  17. (destructuring-bind (&optional first &rest rest) list
  18. (when first
  19. (write-string first stream)
  20. (when rest
  21. (write-char delimiter stream)
  22. (join-to-stream stream rest delimiter)))))
  23. (defun join (list &optional (delimiter #\ ))
  24. (with-output-to-string (stream)
  25. (join-to-stream stream list delimiter)))
  26. ;; https://stackoverflow.com/a/34628127
  27. (defun string-contains (string1 string2)
  28. (cond
  29. ((zerop (length string1)) nil) ; string1 is empty (no need to test it every time)
  30. ((> (length string1) (length string2)) nil) ; string1 is longer than string2
  31. ((string= string1 (subseq string2 0 (length string1))) string1) ; string2 starts with string1
  32. (t (string-contains string1 (subseq string2 1))))) ; otherwise shorten string2 by 1 and start over
  33. (defun range (max &key (min 0) (step 1))
  34. "Get a list of integers."
  35. (loop for n from min below max by step
  36. collect n))
  37. (defun rename-group (old-name new-name)
  38. (or (find-group (current-screen) new-name)
  39. (%grename new-name (find-group (current-screen) old-name))))
  40. (defun current-window-width ()
  41. (format-expand *window-formatters* "%w" (current-window)))
  42. (defun current-window-height ()
  43. (format-expand *window-formatters* "%h" (current-window)))
  44. (defun auto-pull-frames ()
  45. (mapcar #'(lambda (frame)
  46. (pull-window-by-number frame)
  47. (fnext))
  48. (range 15 :min 0 :step 1)))
  49. (defun single-quote-string (str)
  50. (let ((string-quote "'"))
  51. (concat string-quote str string-quote)))
  52. (defun quote-string (str)
  53. (let ((string-quote "\""))
  54. (concat string-quote str string-quote)))
  55. (defun time-date-and-time-restrict ()
  56. (time-format "%Y-%m-%d-%H-%M-%S"))