2020-04-14 20:09:47 +01:00
|
|
|
;;; Utils --- Utils library of FG42 -*- lexical-binding: t; -*-
|
|
|
|
;;
|
2024-02-18 16:08:02 +00:00
|
|
|
;; Copyright (c) 2010-2024 Sameer Rahmani <lxsameer@gnu.org>
|
2020-04-14 20:09:47 +01:00
|
|
|
;;
|
|
|
|
;; Author: Sameer Rahmani <lxsameer@gnu.org>
|
2022-12-19 21:07:48 +00:00
|
|
|
;; URL: https://devheroes.codes/FG42/FG42
|
2024-04-05 21:04:36 +01:00
|
|
|
;; Version: 4.0.0
|
2020-04-14 20:09:47 +01:00
|
|
|
;;
|
|
|
|
;; 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 <http://www.gnu.org/licenses/>.
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
;; `System' is just a state monad which holds the state of the editor.
|
|
|
|
;; Each system has to have a `start' function to start the setup process.
|
|
|
|
;;
|
|
|
|
;;; Code:
|
|
|
|
(require 'cl-lib)
|
|
|
|
|
2020-04-15 12:08:13 +01:00
|
|
|
(autoload 'seq-partition "seq")
|
|
|
|
(autoload 'cl-reduce "cl-seq")
|
|
|
|
|
|
|
|
|
2020-04-14 20:09:47 +01:00
|
|
|
(defun buffer-mode (buffer-or-string)
|
|
|
|
"Return the major mode associated with a the given BUFFER-OR-STRING."
|
|
|
|
(with-current-buffer buffer-or-string
|
|
|
|
major-mode))
|
|
|
|
|
|
|
|
|
|
|
|
(defun ->buffer (buffer-name data &optional fn)
|
|
|
|
"Insert the given DATA into the given buffer provided by BUFFER-NAME.
|
|
|
|
|
|
|
|
It will create a the buffer if it doesn't exist. It will call the given FN
|
|
|
|
at the end in context of the buffer. This function accepts only one argument
|
|
|
|
with is the buffer."
|
|
|
|
(let ((buf (get-buffer-create buffer-name)))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(insert data)
|
|
|
|
(when fn
|
|
|
|
(funcall fn buf)))))
|
|
|
|
|
|
|
|
|
2020-04-15 12:08:13 +01:00
|
|
|
(defun ->str (&rest args)
|
|
|
|
"Convert the given ARGS into string."
|
|
|
|
(funcall #'pp-to-string args))
|
|
|
|
|
|
|
|
|
2020-04-14 20:09:47 +01:00
|
|
|
(defmacro inspect-expression (&rest body)
|
|
|
|
"Pretty prints the result of the given BODY."
|
|
|
|
`(pp-display-expression ,@body (get-buffer-create fg42/inspect-buffer)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun inspect-data-append (data)
|
|
|
|
"Append the given DATA to the inspection buffer with padding."
|
|
|
|
;; TODO: Move 'fg42/inspect-buffer' to the somewhere propriate
|
|
|
|
;; possiblly the system.
|
|
|
|
(->buffer
|
|
|
|
"fg42/inspect-buffer"
|
|
|
|
(format
|
|
|
|
"\n;; START ======================================================\n%s%s"
|
|
|
|
(pp-to-string data)
|
|
|
|
";; END.\n")))
|
|
|
|
|
|
|
|
|
|
|
|
(defun apply-face (face-symbol text)
|
|
|
|
"Apply the given FACE-SYMBOL to the given TEXT."
|
|
|
|
(put-text-property 0 (length text) 'face face-symbol text))
|
|
|
|
|
|
|
|
|
2020-04-15 12:08:13 +01:00
|
|
|
(defmacro comment (&rest _body)
|
2020-04-14 20:09:47 +01:00
|
|
|
"A macro similar to Clojure's comment macro that ignore the BODY."
|
|
|
|
(declare (indent 0))
|
|
|
|
`nil)
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro debug-message (&rest params)
|
|
|
|
"Print out the given PARAMS only if debug mode is on."
|
|
|
|
(if debug-on-error
|
|
|
|
`(message ,@params)
|
|
|
|
nil))
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro deprecated (msg &rest form)
|
|
|
|
"Mark the given FORM as deprecated with the given MSG."
|
|
|
|
(declare (indent 0))
|
|
|
|
`(progn
|
|
|
|
(warn (format "[DEPRECATED]: %s" ,msg))
|
|
|
|
,@form))
|
|
|
|
|
|
|
|
|
2020-04-16 18:17:36 +01:00
|
|
|
(defun path-join (&rest paths)
|
|
|
|
"Join the given PATHS."
|
|
|
|
(apply #'concat
|
|
|
|
(append
|
|
|
|
(mapcar #'file-name-as-directory (butlast paths))
|
|
|
|
(last paths))))
|
|
|
|
|
|
|
|
|
2020-10-24 19:05:39 +01:00
|
|
|
(defmacro -> (x &optional form &rest more)
|
|
|
|
"Thread the expr through the forms FORM and rest of form in MORE.
|
|
|
|
Insert X as the second item in the first form, making a list of
|
|
|
|
it if it is not a list already. If there are more forms, insert
|
|
|
|
the first form as the second item in second form, etc."
|
|
|
|
(declare (debug (form &rest [&or symbolp (sexp &rest form)])))
|
|
|
|
(cond
|
|
|
|
((null form) x)
|
|
|
|
((null more) (if (listp form)
|
|
|
|
`(,(car form) ,x ,@(cdr form))
|
|
|
|
(list form x)))
|
|
|
|
(:else `(-> (-> ,x ,form) ,@more))))
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro ->> (x &optional form &rest more)
|
|
|
|
"Thread the expr through the forms FORM and the rest at MORE.
|
|
|
|
Insert X as the last item in the first form, making a list of
|
|
|
|
it if it is not a list already. If there are more forms, insert
|
|
|
|
the first form as the
|
|
|
|
last item in second form, etc."
|
|
|
|
(declare (debug ->))
|
|
|
|
(cond
|
|
|
|
((null form) x)
|
|
|
|
((null more) (if (listp form)
|
|
|
|
`(,@form ,x)
|
|
|
|
(list form x)))
|
|
|
|
(:else `(->> (->> ,x ,form) ,@more))))
|
|
|
|
|
|
|
|
|
2021-12-11 14:56:57 +00:00
|
|
|
(defmacro when-wm (&rest body)
|
|
|
|
"Run the BODY only if in wm mode."
|
|
|
|
(if (string= (getenv "FG42_WM") "true")
|
|
|
|
`(progn ,@body)
|
|
|
|
nil))
|
|
|
|
|
2022-06-27 23:41:24 +01:00
|
|
|
(defmacro when-not-wm (&rest body)
|
|
|
|
"Run the BODY only if not in the wm mode."
|
|
|
|
(if (not (string= (getenv "FG42_WM") "true"))
|
|
|
|
`(progn ,@body)
|
|
|
|
nil))
|
|
|
|
|
2021-12-11 14:56:57 +00:00
|
|
|
|
2024-04-07 14:12:37 +01:00
|
|
|
(defun get-base16-color-or (color-name default)
|
|
|
|
"Return the color for COLOR-NAME if a base16 theme is loade otherwise DEFAULT."
|
|
|
|
(let* ((theme (car custom-enabled-themes))
|
|
|
|
(theme-sym (intern (format "%s-theme-colors" theme))))
|
|
|
|
|
|
|
|
(if (boundp theme-sym)
|
|
|
|
(or (eval `(plist-get ,theme-sym ,color-name)) default)
|
|
|
|
default)))
|
|
|
|
|
|
|
|
|
2020-04-14 20:09:47 +01:00
|
|
|
(provide 'fg42/utils)
|
|
|
|
;;; utils.el ends here
|