1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-07-27 01:09:44 +01:00
Files
.github
assets
build
diagnostics
doc
examples
src
tests
benchmarks
examples
mocked-pagers
scripts
snapshots
syntax-tests
highlighted
ARM Assembly
ASP
AWK
ActionScript
Ada
Apache
AppleScript
AsciiDoc
Assembly (x86_64)
Bash
BatTestCustomAssets
Batch
BibTeX
C
C-Sharp
CMake
CSS
CSV
Cabal
Clojure
CoffeeScript
Cpp
CpuInfo
Crontab
Crystal
D
Dart
Diff
Dockerfile
DotENV
Elixir
Elm
Email
Erlang
EtcGroup
F#
Fish
Fortran (Fixed Form)
Fortran (Modern)
Fortran Namelist
Fstab
GLSL
Git Attributes
Git Config
Git Ignore
Go
GraphQL
Graphviz DOT
Groff
Groovy
HTML
Haskell
Hosts
INI
Ignored suffixes
JQ
JSON
Java
Java Server Page (JSP)
JavaScript
Jinja2
Julia
Kotlin
LLVM
Lean
Less
Lisp
utils.lisp
Literate Haskell
LiveScript
Log
Lua
MATLAB
Makefile
Manpage
Markdown
MediaWiki
MemInfo
NAnt Build File
NSE
NSIS
Ninja
OCaml
Objective-C
Objective-C++
PHP
Pascal
Passwd
Perl
Plaintext
PowerShell
Protocol Buffer
Puppet
PureScript
Python
QML
R
Racket
Rego
Regular Expression
Requirements.txt
Robot Framework
Ruby
Ruby Haml
Ruby On Rails
Rust
SCSS
SLS
SML
SQL
SSH Config
SSHD Config
Sass
Scala
Slim
Solidity
Strace
Stylus
Svelte
Swift
Syslog
SystemVerilog
TOML
Tcl
TeX
Terraform
Textile
Todo.txt
TypeScript
TypeScriptReact
Verilog
VimHelp
VimL
Vue
Vyper
WGSL
XAML
XML
YAML
Zig
cmd-help
dash
fish_history
gnuplot
http-request-response
jsonnet
nginx
nim
nix
orgmode
reStructuredText
resolv.conf
varlink
source
BatTestCustomAssets.sublime-syntax
compare_highlighted_versions.py
create_highlighted_versions.py
regression_test.sh
test_custom_assets.sh
update.sh
tester
utils
.gitattributes
assets.rs
github-actions.rs
integration_tests.rs
no_duplicate_extensions.rs
snapshot_tests.rs
system_wide_config.rs
test_pretty_printer.rs
.gitignore
.gitmodules
CHANGELOG.md
CONTRIBUTING.md
Cargo.lock
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
NOTICE
README.md
rustfmt.toml
bat/tests/syntax-tests/highlighted/Lisp/utils.lisp
2020-10-16 08:58:04 +02:00

81 lines
20 KiB
Common Lisp
Vendored
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(cl:defpackage :chillax.utils
 (:use :cl :alexandria)
 (:export
 :fun :mkhash :hashget :strcat :dequote :at))
(in-package :chillax.utils)
;;; Functions
(defmacro fun (&body body)
 "This macro puts the FUN back in FUNCTION."
 `(lambda (&optional _) (declare (ignorable _)) ,@body))
;;; Hash tables
(defun mkhash (&rest keys-and-values &aux (table (make-hash-table :test #'equal)))
 "Convenience function for `literal' hash table definition."
 (loop for (key val) on keys-and-values by #'cddr do (setf (gethash key table) val)
 finally (return table)))
(defun hashget (hash &rest keys)
 "Convenience function for recursively accessing hash tables."
 (reduce (lambda (h k) (gethash k h)) keys :initial-value hash))
(define-compiler-macro hashget (hash &rest keys)
 (if (null keys) hash
 (let ((hash-sym (make-symbol "HASH"))
 (key-syms (loop for i below (length keys)
 collect (make-symbol (format nil "~:@(~:R~)-KEY" i)))))
 `(let ((,hash-sym ,hash)
 ,@(loop for key in keys for sym in key-syms
 collect `(,sym ,key)))
 ,(reduce (lambda (hash key) `(gethash ,key ,hash))
 key-syms :initial-value hash-sym)))))
(defun (setf hashget) (new-value hash key &rest more-keys)
 "Uses the last key given to hashget to insert NEW-VALUE into the hash table
returned by the second-to-last key.
tl;dr: DWIM SETF function for HASHGET."
 (if more-keys
 (setf (gethash (car (last more-keys))
 (apply #'hashget hash key (butlast more-keys)))
 new-value)
 (setf (gethash key hash) new-value)))
;;; Strings
(defun strcat (string &rest more-strings)
 (apply #'concatenate 'string string more-strings))
(defun dequote (string)
 (let ((len (length string)))
 (if (and (> len 1) (starts-with #\" string) (ends-with #\" string))
 (subseq string 1 (- len 1))
 string)))
;;;
;;; At
;;;
(defgeneric at (doc &rest keys))
(defgeneric (setf at) (new-value doc key &rest more-keys))
(defmethod at ((doc hash-table) &rest keys)
 (apply #'hashget doc keys))
(defmethod (setf at) (new-value (doc hash-table) key &rest more-keys)
 (apply #'(setf hashget) new-value doc key more-keys))
(defmethod at ((doc list) &rest keys)
 (reduce (lambda (alist key)
 (cdr (assoc key alist :test #'equal)))
 keys :initial-value doc))
(defmethod (setf at) (new-value (doc list) key &rest more-keys)
 (if more-keys
 (setf (cdr (assoc (car (last more-keys))
 (apply #'at doc key (butlast more-keys))
 :test #'equal))
 new-value)
 (setf (cdr (assoc key doc :test #'equal)) new-value)))
;; A playful alias.
(defun @ (doc &rest keys)
 (apply #'at doc keys))
(defun (setf @) (new-value doc key &rest more-keys)
 (apply #'(setf at) new-value doc key more-keys))