Compare commits

...

3 Commits

Author SHA1 Message Date
Daniel - 7db9f8d56c
Fix indentation in utility library to conform to Common Lisp
This is the current indentation default.
2023-04-30 17:28:23 +02:00
Daniel - 28b9918325
Add utility function to create selector functions from table headers
This might be useful to work with table data from Org tables in source blocks,
maybe.

Does this exist somewhere already?
2023-04-30 17:26:56 +02:00
Daniel - 7bd8c0ef2b
Remove obsolete function to conditionally enable lispy
Not using this anymore.
2023-04-30 17:25:53 +02:00
2 changed files with 79 additions and 35 deletions

View File

@ -589,7 +589,6 @@
db/run-or-hide-ansi-term
db/hex-to-ascii
db/text-to-hex
conditionally-enable-lispy
turn-on-lispy-when-available
turn-on-flycheck-when-file
db/sort-nsm-permanent-settings
@ -612,7 +611,8 @@
db/sync-magit-repos-from-projectile
db/replace-variables-in-string
db/dired-ediff-files
db/grep-read-files))
db/grep-read-files
db/make-selector-from-table-header))
(use-package db-hydras
:commands (hydra-toggle/body

View File

@ -250,16 +250,6 @@ FORMAT-STRING defaults to some ISO 8601-like format."
(message time-string)
time-string)))
(defun conditionally-enable-lispy ()
"Enable lispy-mode when in `eval-expression or in
`pp-eval-expression. lispy must have been loaded for this
first, i.e., this function will not automatically load
lispy."
(when (and (featurep 'lispy)
(or (eq this-command 'eval-expression)
(eq this-command 'pp-eval-expression)))
(lispy-mode 1)))
(defun turn-on-lispy-when-available ()
"Activate `lispy in current buffer when possible.
Will print a warning in case of failure."
@ -525,30 +515,30 @@ entries, even if I want to use the input directly."
(stringp bn)
(file-name-nondirectory bn)))
(default-alias
(and fn
(let ((aliases (remove (assoc "all" grep-files-aliases)
grep-files-aliases))
alias)
(while aliases
(setq alias (car aliases)
aliases (cdr aliases))
(if (string-match (mapconcat
#'wildcard-to-regexp
(split-string (cdr alias) nil t)
"\\|")
fn)
(setq aliases nil)
(setq alias nil)))
(cdr alias))))
(and fn
(let ((aliases (remove (assoc "all" grep-files-aliases)
grep-files-aliases))
alias)
(while aliases
(setq alias (car aliases)
aliases (cdr aliases))
(if (string-match (mapconcat
#'wildcard-to-regexp
(split-string (cdr alias) nil t)
"\\|")
fn)
(setq aliases nil)
(setq alias nil)))
(cdr alias))))
(default-extension
(and fn
(let ((ext (file-name-extension fn)))
(and ext (concat "*." ext)))))
(and fn
(let ((ext (file-name-extension fn)))
(and ext (concat "*." ext)))))
(default
(or default-alias
default-extension
(car grep-files-history)
(car (car grep-files-aliases))))
(or default-alias
default-extension
(car grep-files-history)
(car (car grep-files-aliases))))
(files (completing-read
(format "Search for \"%s\" in files matching wildcard: "
regexp)
@ -562,6 +552,60 @@ entries, even if I want to use the input directly."
(or (cdr (assoc files grep-files-aliases))
files))))
(defun db/make-selector-from-table-header (header)
"Return selector function based on names contained in HEADER.
A selector function is a function that receives a KEY (a symbol)
and a ROW (list of values) and returns the value in ROW with the
same index that KEY has in HEADER. A use-case for such a
selector function is to have a table represented as a list of
lists (rows), where the first list (row) is the header and all
subsequent lists (rows) are the actual values; to access values
in all subsequent rows by name, one can use a selector function
on the header to do so.
HEADER must be a list of strings or symbols and must not contain
duplicates when elements are considered as symbols."
(unless (listp header)
(user-error "Header is not a list, cannot create selector"))
(unless (-all? (-orfn #'stringp #'symbolp) header)
(user-error "Header must consist of strings or symbols, cannot create selector"))
(let ((header (-map #'(lambda (elt)
(cond
((symbolp elt) elt)
((stringp elt) (intern (downcase elt)))))
header)))
;; Check for duplicates in HEADER
(when (-reduce-from #'(lambda (val tail)
(or val (memq (cl-first tail)
(cl-rest tail))))
nil
(-tails header))
(user-error "Header contains duplicates, cannot create selector"))
;; Return actual selector
(let* ((lookup-table (make-hash-table)))
(mapc #'(lambda (idx)
(puthash (nth idx header)
idx
lookup-table))
(-iota (length header)))
#'(lambda (column row)
(let ((key (if (symbolp column)
column
(user-error "Unknow key type %s of key %s"
(type-of column)
column))))
(if-let ((idx (gethash key lookup-table)))
(nth idx row)
(user-error "Unknow column name %s" column)))))))
;;; Base45 Decoding
@ -573,7 +617,7 @@ entries, even if I want to use the input directly."
(-each-indexed (string-to-list base45-alphabet)
(-lambda (index char)
(puthash char index decode-hash-table)
(puthash char index decode-hash-table)
;; Add an encode-hash-table here in case base45-encode-string will ever be
;; written, like so: (puthash index char encode-hash-table)
))