Adapation du rc.elv au nouvel emplacement de mon module perso + suppression de l’ancien

This commit is contained in:
Benjamin VAUDOUR 2021-12-28 11:28:59 +00:00
parent ca4b6e3c75
commit f5bcaaf2ec
22 changed files with 16 additions and 1981 deletions

View File

@ -1,6 +0,0 @@
var dir = ~/.config/elvish/aliases
for file [(set _ = ?(put $dir/*.elv))] {
var content = (cat $file | slurp)
eval $content
}

View File

@ -1,19 +0,0 @@
#use github.com/zzamboni/elvish-completions/builtins
#use github.com/zzamboni/elvish-completions/cd
#use github.com/zzamboni/elvish-completions/git
#set git:git-command = hub
#set git:git-completions[checkout] = [ { git:MODIFIED; git:BRANCHES &all } ... ]
#git:init
#use github.com/zzamboni/elvish-completions/ssh
#use github.com/xiaq/edit.elv/compl/go
use ./completion/arc
use ./completion/archiver
use ./completion/desarchiver
use ./completion/kcp
use ./completion/mpv
use ./completion/pacman
use ./completion/ssh
use ./completion/sudo
use ./completion/use

View File

@ -1,48 +0,0 @@
use moi/util/condition
use moi/util/file
var commands = [
help
archive
unarchive
extract
ls
]
var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ]
fn -comp-commands { all $commands }
fn -comp-archive-files {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions
}
fn -comp-inline-files {|archive|
try {
arc ls $archive | eawk {|_ @argv| put $argv[-1] }
} except e {
nop
}
}
fn complete {|@argv|
var c cmd = (count $argv) $argv[1]
if (== $c 2) {
-comp-commands
} elif (== $c 3) {
if (not (has-value [help archive] $cmd)) {
-comp-archive-files $argv[-1]
}
} else {
if (eq $cmd archive) {
edit:complete-filename $@argv
} elif (eq $cmd extract) {
var archive = $argv[2]
-comp-inline-files $archive
}
}
}
set edit:completion:arg-completer[arc] = $complete~

View File

@ -1,30 +0,0 @@
var options = [
z
l
x
b
g
h
]
var description = [
&z='zst compression (default)'
&l='lz4 compression'
&x='xz compression'
&b='bz2 compression'
&g='gzip compression'
&h='display help'
]
fn -options {
each {|o|
put [&short=$o &desc=$description[$o]]
} $options
}
fn complete {|@argv|
edit:complete-getopt $argv [(-options)] [ $edit:complete-filename~ ...]
}
set edit:completion:arg-completer[archiver] = $complete~

View File

@ -1,12 +0,0 @@
use moi/util/condition
use moi/util/file
var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ]
fn complete {|@argv|
var m = $argv[-1]
var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions
}
set edit:completion:arg-completer[desarchiver] = $complete~

View File

@ -1,41 +0,0 @@
fn -remotes-packages { kcp -lN }
var options = [
-h
-v
-i
-di
-u
-l
-lN
-lS
-lI
-lO
-lx
-lxS
-lxI
-lxO
-lf
-s
-g
-V
]
var np = [
-i
-di
-s
-g
-V
]
fn complete {|@argv|
var c = (count $argv)
if (== $c 2) {
all $options
} elif (and (== $c 3) (has-value $np $argv[-2])) {
-remotes-packages
}
}
set edit:completion:arg-completer[kcp] = $complete~

View File

@ -1,59 +0,0 @@
use moi/util/condition
use moi/util/file
var extensions = [
aac
ape
avi
divx
flac
flv
m3u
m4a
m4v
mp3
mp4
mpeg
mpg
mkv
mng
mov
qt
oga
ogg
ogm
ogv
opus
ra
rv
ts
vob
wav
webm
wmv
wma
wmx
]
fn -files {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions
}
fn complete {|@argv|
var c = (count $argv)
if (== $c 2) {
put --speed
-files $argv[-1]
} elif (== $c 3) {
if (eq $argv[-2] --speed) {
put 0.8 0.9 1.0 1.1 1.2
} else {
-files $argv[-1]
}
} else {
-files $argv[-1]
}
}
set edit:completion:arg-completer[mpv] = $complete~

View File

@ -1,105 +0,0 @@
use moi/util/condition
use moi/util/file
use moi/util/list
fn -local-packages { pacman -Q | eawk {|_ p @_| put $p } }
fn -repo-packages {
var packages = [(pacman -Ss | list:ploop &step=2 {|_ v|
put $v
} | eawk {|_ p @_|
put $p
})]
var spackages = [&]
use str
peach {|p|
str:split '/' $p
} $packages | peach {|e|
set spackages[$e] = $nil
}
keys $spackages
all $packages
}
fn -downloaded-packages {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m tar.zst tar.xz tar.gz tar.bz2
}
fn -local-file {|m| edit:complete-filename $m }
var options = [
-h
-V
-Q
-Qs
-Ql
-Qi
-Qm
-Qdt
-Qo
-R
-Rsn
-S
-Ss
-Si
-Sii
-Syu
-Syyu
-U
-D
]
var asdeps = [
-S
-U
-D
]
var lpack = [
-Q
-Qs
-Ql
-Qi
-D
-R
-Rsn
]
var rpack = [
-S
-Ss
-Si
-Sii
]
var dpack = [
-U
]
var fpack = [
-Qo
]
fn complete {|@argv|
var c = (count $argv)
if (< $c 3) {
all $options
} else {
var cmd = $argv[1]
if (and (== $c 3) (has-value $asdeps $cmd)) {
put --asdeps --asexplicit
}
if (has-value $lpack $cmd) {
-local-packages
} elif (has-value $rpack $cmd) {
-repo-packages
} elif (has-value $dpack $cmd) {
-downloaded-packages $argv[-1]
} elif (has-value $fpack $cmd) {
-local-file $argv[-1]
}
}
}
set edit:completion:arg-completer[pacman] = $complete~

View File

@ -1,102 +0,0 @@
use re
use str
use moi/util/condition
use moi/util/file
use moi/util/ip
use moi/util/list
use moi/util/map
use moi/util/option
var options-ssh = [ 1 2 4 6 A D f g I i L l m o v a b C c e F k N n p q R s T t X x ]
var options-scp = [ 3 4 6 B C p q r v c F i l o P S ]
var names = [
benjamin
luence
]
fn -kh {
cat ~/.ssh/known_hosts | peach {|l|
put [(str:split ' ' $l)]
} | peach {|e|
var domains @_ = $@e
str:split ',' $domains
}
}
fn -port {|cmd @argv|
var o = (condition:cset (eq $cmd 'ssh') '-p' '-P')
var margs = (option:map $argv)
var p = (map:value-of $margs $o &default=[])
if (list:empty $p) {
put 22
} else {
put $p[-1]
}
}
fn -complete-names {|| each {|n| put $n'@' } $names }
fn -complete-domains {|name hosts|
each {|h|
put $name@$h
} $hosts
}
fn -complete-remote-dir {|port address dir|
var cmd = (printf ^
'for f in %s*; do if [[ -d $f ]]; then echo $f/; else echo $f; fi; done' ^
$dir)
try {
ssh -p $port $address $cmd | each {|f|
put $address':'$f
}
} except e {
}
}
fn -complete-args {|hosts cmd @argv|
var arg = $argv[-1]
var i = (str:index $arg @)
if (< $i 0) {
-complete-names
all $hosts
if (eq $cmd scp) {
edit:complete-filename $cmd $@argv
}
return
}
var n h = $arg[..$i] $arg[(+ $i 1)..]
if (eq $cmd scp) {
set i = (str:index $h :)
if (>= $i 0) {
var d = $h[(+ $i 1)..]
set h = $h[..$i]
if (list:includes $h $hosts) {
var p = (-port $cmd @argv)
-complete-remote-dir $p $n@$h $d
}
return
}
}
-complete-domains $n $hosts
}
fn complete {|@argv|
var @hosts = (-kh)
var cmd = $argv[0]
var is-ssh = (eq $cmd ssh)
var po = (condition:cset $is-ssh -p -P)
if (<= (count $argv) 2) {
all (condition:cset $is-ssh $options-ssh $options-scp)
-complete-args $hosts $@argv
} elif (eq $argv[-2] $po) {
put 22
} else {
-complete-args $hosts $@argv
}
}
set edit:completion:arg-completer[scp] = $complete~
set edit:completion:arg-completer[ssh] = $complete~

View File

@ -1,10 +0,0 @@
fn complete {|@argv|
if (and (> (count $argv) 2) (has-key $edit:completion:arg-completer $argv[1])) {
$edit:completion:arg-completer[$argv[1]] (all $argv[1:])
} else {
edit:complete-sudo $@argv
}
}
set edit:completion:arg-completer[sudo] = $edit:complete-sudo~
#edit:completion:arg-completer[sudo] = $-complete~

View File

@ -1,24 +0,0 @@
var libdir = $E:HOME/.config/elvish/lib
var builtin_modules = [
builtin
epm
file
math
path
re
readline-binding
store
str
unix
]
set edit:completion:arg-completer[use] = {|@argv|
use str
use path
all $builtin_modules
put $libdir/**.elv | each {|f|
if (path:is-regular $f) {
str:trim-prefix $f $libdir/
}
} | each {|f| str:trim-suffix $f .elv }
}

View File

@ -1,42 +0,0 @@
edit:add-var ++~ {|n| + $n 1 }
edit:add-var --~ {|n| + $n 1 }
edit:add-var sign~ {|n|
if (> $n 0) {
put 1
} elif (< $n 0) {
put -1
} else {
put 0
}
}
edit:add-var neg~ {|n| * $n -1 }
edit:add-var abs~ {|n|
if (< $n 0) {
neg $n
} else {
put $n
}
}
edit:add-var sum~ {|@numbers|
var s = 0
each {|e| set s = (+ $s e) } (to-list $numbers)
}
fn -minmax {|t @numbers|
var l = (to-list $@numbers)
if (== (count $l) 0) {
return
}
var m = $l[0]
each {|n|
if ($t $n $m) {
set m = $n
}
} $l[1..]
put $m
}
edit:add-var min~ {|@numbers| -minmax $'<~' $@numbers }
edit:add-var max~ {|@numbers| -minmax $'>~' $@numbers }

View File

@ -1,20 +0,0 @@
fn cset {|c t f|
if $c {
put $t
} else {
put $f
}
}
fn mset {|c t f|
all (cset $c $t $f)
}
fn call {|c t f @argv|
var v = (cset $c $t $f)
if (is (kind-of $v) fn) {
$v $@argv
} else {
put $v
}
}

View File

@ -1,58 +0,0 @@
use str
use ./list
use ./map
var search-type = [
&exact= {|m| ls $m 2>/dev/null }
&match= {|m| put *$m* }
&prefix= {|m| put $m* }
&suffix= {|m| put *$m }
&deep-match= {|m| put **$m** }
&deep-prefix= {|m| put $m** }
&deep-suffix= {|m| put **$m }
]
fn -less {|f1 f2|
var fl1 fl2 = (str:to-lower $f1) (str:to-lower $f2)
var c = (str:compare $fl1 $fl2)
if (== $c 0) {
set c = (str:compare $f1 $f2)
}
put (< $c 0)
}
fn -get-results {|sort result|
if $sort {
keys $result | order $-less~
} else {
keys $result
}
}
fn -search {|&sort=$false &type=exact @motive|
var f result = $search-type[$type] [&]
if (list:empty $motive) {
set result = (put * | map:to-set)
} else {
peach {|m|
try {
$f $m | peach {|e|
set result[$e] = $nil
}
} except e { }
} $motive
}
-get-results $sort $result
}
fn match-files {|&sort=$false &type=prefix @motive| -search &sort=$sort &type=$type $@motive }
fn match-extensions {|&sort=$false &type=deep-prefix motive @extensions|
var result = [&]
-search &type=$type $motive | peach {|f|
if (list:contains {|e| str:has-suffix $f .$e } $extensions) {
set result[$f] = $nil
}
}
-get-results $sort $result
}

View File

@ -1,203 +0,0 @@
use math
use re
use str
fn chars {|v|
str:split '' $v
}
fn len {|v|
chars $v | count
}
fn string {|v|
var k = (kind-of $v)
if (eq $k 'string') {
put $v
} elif (eq $k 'number') {
to-string $v
} elif (eq $k 'bool') {
if $v {
put 'X'
} else {
put ''
}
} elif (eq $k 'list') {
each $string~ $v | str:join ''
} elif (eq $v $nil) {
put ''
} else {
to-string $v
}
}
fn int {|v|
var k = (kind-of $v)
if (eq $k 'string') {
try {
var n = (math:trunc $v)
var s @_ = (str:split '.' (to-string $n))
put $s
} except e {
fail (printf '%s nest pas un nombre' $v)
}
} elif (eq $k 'number') {
int (to-string $v)
} elif (eq $k 'bool') {
if $v {
put 1
} else {
put 0
}
} elif (eq $v $nil) {
put 0
} else {
put 0
}
}
fn repeat {|n s|
use builtin
builtin:repeat $n $s | str:join ''
}
fn blank {|n|
repeat $n ' '
}
fn left {|str size|
var l = (len $str)
if (< $l $size) {
string [ $str (blank (- $size $l)) ]
} elif (== $l $size) {
put $str
} else {
string [ (chars $str) ][..$size]
}
}
fn right {|str size|
var l = (len $str)
if (< $l $size) {
string [ (blank (- $size $l)) $str ]
} elif (== $l $size) {
put $str
} else {
string [ (chars $str) ][(- $l $size)..]
}
}
fn center {|str size|
var l = (len $str)
if (< $l $size) {
var d = (- $size $l)
var b = (math:trunc (/ $d 2))
var e = (- $d $b)
string [ (blank $b) $str (blank $e) ]
} elif (== $l $size) {
put $str
} else {
var d = (- $l $size)
var b = (math:trunc (/ $d 2))
var e = (+ $b $size)
string [ (chars $str) ][$b..$e]
}
}
var -align = [
&center=$center~
&right=$right~
&left=$left~
]
fn column {|&align=left name @label|
var c = [
&name=$name
&align=$-align[$align]
&size=0
&label=''
]
if (> (count $label) 0) {
set c[label] = $label[0]
set c[size] = (count $label[0])
}
put $c
}
fn update-props {|props data|
each {|d|
set @props = (each {|c|
var n = $c[name]
if (has-key $d $n) {
var v = (string $d[$n])
var l = (count $v)
if (< $c[size] $l) {
set c[size] = $l
}
}
put $c
} $props)
} $data
put $props
}
fn line-header {|&sep=' ' props|
var @data = (each {|c|
$c[align] $c[label] $c[size]
} $props)
str:join $sep $data
}
fn line {|&sep=' ' props line|
var @data = (each {|c|
var n v = $c[name] ''
if (has-key $line $n) {
set v = (string $line[$n])
}
$c[align] $v $c[size]
} $props)
str:join $sep $data
}
fn list {|&with-header=$true &csep=' ' &hsep='-' &recompute=$true props data|
if $recompute {
set props = (update-props $props $data)
}
if $with-header {
echo (line-header &sep=$csep $props)
if (bool $hsep) {
var s = (* (- (count $props) 1) (count $csep))
each {|c|
set s = (+ $s $c[size])
} $props
echo (repeat (math:trunc (/ $s (count $hsep))) $hsep)
}
}
each {|d|
echo (line &sep=$csep $props $d)
} $data
}
fn size {|size|
var u = 0
var m = [
&10=Kio
&20=Mio
&30=Gio
]
while (< $u 30) {
var p = (math:pow 2 (+ $u 10))
if (< $size $p) {
break
}
set u = (to-string (+ $u 10))
}
if (== $u 0) {
put $size
} else {
var p = (math:pow 2 $u)
var e = (/ $size $p)
printf '%.1f%s' $e $m[$u]
}
}

View File

@ -1,153 +0,0 @@
use re
use str
fn is-ipv4 {|arg|
if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) {
put $false
} else {
use ./list
not (str:split '.' $arg | list:contains {|p| > $p 255 })
}
}
fn is-ipv6 {|arg|
var p = '[0-9a-fA-F]{1,4}'
var g1 g2 = '('$p':)' '(:'$p')'
var cases = [
$g1'{7,7}'$p
$g1'{1,7}:'
$g1'{1,6}:'$p
$g1'{1,5}'$g2'{1,2}'
$g1'{1,4}'$g2'{1,3}'
$g1'{1,3}'$g2'{1,4}'
$g1'{1,2}'$g2'{1,5}'
$p':'$g2'{1,6}'
':'$g2'{1,7}'
'::'
]
var r = '^('(str:join '|' $cases)')$'
put (re:match $r $arg)
}
fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
fn -long-part6 {|p|
use str
set p = (str:to-lower $p)
var c = (- 4 (count $p))
put (repeat $c '0' | str:join '')$p
}
fn -middle-part6 {|p|
while (and (> (count $p) 1) (eq $p[0] 0)) {
set p = $p[1..]
}
put $p
}
fn -find-max0 {|parts|
use ./list
var idx s = -1 1
var ci cs f = -1 0 $false
list:loop {|i p|
if (eq $p 0) {
condition:call $f { set cs = (+ $cs 1) } { set ci cs f = $i 1 $true }
} elif $f {
set f = $false
if (> $cs $s) {
set idx s = $ci $cs
}
}
} $parts
if (and $f (> $cs $s)) {
set idx s = $ci $cs
}
put $idx $s
}
fn long6 {|ip|
if (not (is-ipv6 $ip)) {
fail 'Not an IPv6'
}
if (eq $ip '::') {
repeat 8 '0000' | str:join ':'
return
}
var c = (- 7 (str:count $ip ':'))
if (> $c 0) {
var i = (str:index $ip '::')
var z = (repeat $c ':' | str:join '')
set ip = (str:join '' [$ip[..$i] $z $ip[{$i}..]])
}
str:split ':' $ip | each $-long-part6~ | str:join ':'
}
fn middle6 {|ip|
str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':'
}
fn short6 {|ip|
var parts = [(str:split ':' (middle6 $ip))]
var i s = (-find-max0 $parts)
if (>= $i 0) {
var left right = $parts[..$i] $parts[(+ $i $s)..]
if (== (count $left) 0) {
set left = ['']
}
if (== (count $right) 0) {
set right = ['']
}
set parts = [$@left '' $@right]
}
str:join ':' $parts
}
fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
fn -cmp {|e1 e2|
use ./list
use ./condition
var c = 0
list:loop {|i p1|
var p2 = $e2[$i]
set c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0))
if (!= $c 0) {
break
}
} $e1
put $c
}
fn cmp4 {|ip1 ip2|
if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) {
fail 'Not an IPv4 '$ip1' → '$ip2
}
-cmp [(str:split . $ip1)] [(str:split . $ip2)]
}
fn cmp6 {|ip1 ip2|
if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) {
fail 'Not an IPv6: '$ip1' → '$ip2
}
-cmp [(str:split : (middle6 $ip1))] [(str:split : (middle6 $ip2))]
}
fn cmp {|ip1 ip2|
if (is-ipv4 $ip1) {
if (is-ipv4 $ip2) {
cmp4 $ip1 $ip2
} else {
put -1
}
} elif (is-ipv6 $ip1) {
if (is-ipv4 $ip2) {
put 1
} elif (is-ipv6 $ip2) {
cmp6 $ip1 $ip2
} else {
put -1
}
} else {
fail 'Not an IP: '$ip1' → '$ip2
}
}

View File

@ -1,296 +0,0 @@
use builtin
use str
fn -p {|@argv|
var c = (count $argv)
if (== $c 0) {
put [ (all) ]
} elif (== $c 1) {
put $argv[0]
} else {
fail '0 or 1 argument needed given '$c' arguments: ['(each {|e| put '"'$e'"' } $argv | str:join ', ')']'
}
}
fn -r {|step begin end|
if (> $step 0) {
builtin:range &step=$step $begin (+ $end 1)
} else {
var d = (+ $begin $end)
builtin:range &step=(* $step -1) $end (+ $begin 1) | each {|e| - $d $e }
}
}
fn to-list {|@argv|
try {
-p $@argv
} except e {
put $argv
}
}
fn range {|&step=1 b @r|
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
}
var c = (count $r)
if (> $c 1) {
fail 'usage: list:range &step=1 begin? end'
}
var e = 0
if (== $c 0) {
if (> $step 0) {
set b e = $e $b
}
} else {
set e = $r[0]
}
-r $step $b $e
}
fn indexes {|&from=$false &step=1 @argv|
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
}
var l = (-p $@argv)
var c = (count $l)
if (not $from) {
range &step=$step (- $c 1)
} else {
if (< $from 0) {
set from = (+ $c $from)
}
if (> $step 0) {
if (< $from 0) {
set from = 0
}
range &step=$step $from (- $c 1)
} else {
if (>= $from $c) {
set from = (- $c 1)
}
range &step=$step $from 0
}
}
}
fn -for {|&from=$false &step=1 @argv|
var l = (-p $@argv)
indexes &from=$from &step=$step $l | each {|i| put [ $i $l[$i] ] }
}
fn loop {|&from=$false &step=1 &end=$false cb @argv|
if (not $end) {
-for &from=$from &step=$step $@argv | each {|e| $cb $@e }
} else {
-for &from=$from &step=$step $@argv | each {|e|
var i v = $@e
if ($end $i $v) {
break
}
$cb $i $v
}
}
}
fn ploop {|&from=$false &step=1 cb @argv|
-for &from=$from &step=$step $@argv | peach {|e| $cb $@e }
}
fn reverse {|@argv| loop &step=-1 {|_ e| put $e } $@argv }
fn reach {|cb @argv| reverse $@argv | each {|e| cb $e } }
fn empty {|@argv| == (count (-p $@argv)) 0 }
fn not-empty {|@argv| not (empty $@argv) }
fn filter {|cb @argv|
each {|v|
if ($cb $v) {
put $v
}
} (-p $@argv)
}
fn filter-not {|cb @argv| filter {|v| not ($cb $v) } $@argv }
fn first {|&from=$false &reverse=$false cb @argv|
var f = {|v|
if ($cb $v) {
put $v
break
}
}
var l = (-p $@argv)
if (not $from) {
if $reverse {
reach $f $l
} else {
each $f $l
}
} else {
var c = (count $l)
if (< $from 0) {
set from = (+ $c $from)
}
if $reverse {
var e = (+ $from 1)
if (> $e $c) {
set e = $c
} elif (< $e 0) {
set e = 0
}
reach $f $l[..$e]
} else {
var b = $from
if (> $b $c) {
set b = $c
} elif (< $b 0) {
set b = 0
}
each $f $l[$b..]
}
}
}
fn filter-index {|cb @argv|
loop {|i v|
if ($cb $v) {
put $i
}
} $@argv
}
fn filter-index-not {|cb @argv| filter-index {|v| not ($cb $v) } $@argv }
fn first-index {|&from=$false &reverse=$false cb @argv|
var idx = -1
var step = 1
if $reverse {
set step = -1
}
loop &from=$from &step=$step {|i v|
if ($cb $v) {
set idx = $i
break
}
} $@argv
put $idx
}
fn search {|cb @argv|
loop {|i v|
if ($cb $i $v) {
put $v
}
} $@argv
}
fn search-not {|cb @argv| search {|i v| not ($cb $i $v) } $@argv }
fn search-first {|&from=$false &reverse=$false cb @argv|
var step = 1
if $reverse {
set step = -1
}
loop &from=$from &step=$step {|i v|
if ($cb $i $v) {
put $v
break
}
} $@argv
}
fn search-index {|cb @argv|
loop {|i v|
if ($cb $i $v) {
put $i
}
} $@argv
}
fn search-index-not {|cb @argv| search-index {|i v| not ($cb $i $v) } $@argv }
fn search-first-index {|&from=$false &reverse=$false cb @argv|
var idx = -1
var step = 1
if $reverse {
set step = -1
}
loop &from=$from &step=$step {|i v|
if ($cb $i $v) {
set idx = $i
break
}
} $@argv
put $idx
}
fn contains {|cb @argv|
var e = $false
each {|v|
if ($cb $v) {
set e = $true
break
}
} (-p $@argv)
put $e
}
fn contains-not {|cb @argv| contains {|v| not ($cb $v) } $@argv }
fn exists {|cb @argv|
var e = $false
loop {|i v|
if ($cb $i $v) {
set e = $true
break
}
} $@argv
put $e
}
fn exists-not {|cb @argv| exists {|i v| $cb $i $v } $@argv }
fn includes {|v @argv| contains {|e| is $v $e } $@argv }
fn includes-not {|v @argv| contains-not {|e| is $v $e } $@argv }
fn reduce {|v cb @argv|
each {|e| set v = ($cb $v $e) } (to-list $@argv)
put $v
}
fn remove-duplicate {|@argv|
var done = [&]
each {|v|
put [&v=$v &e=(has-key $done $v)]
set done[$v] = $nil
} (-p $@argv) | each {|v|
assoc $v k (not $v[e])
} | each {|v|
if $v[k] {
put $v[v]
}
}
}
fn premove-duplicate {|@argv|
var done = [&]
peach {|v|
set done[$v] = $nil
} (-p $@argv)
keys $done
}
fn swap {|i j @argv|
var l = (-p $@argv)
var c = (count $l)
set i j = (-i $i $c) (-i $j $c)
if (or (== $i $c) (== $j $c)) {
fail 'Index out of range'
}
if (== $i $j) {
all $l
} else {
if (> $i $j) {
set i j = $j $i
}
take $i $l
put $l[j]
all $l[(+ $i 1)..$j]
put $l[$i]
drop (+ $j 1) $l
}
}
fn less {|cb|
var l = {|a b| < ($cb $a $b) 0 }
put $l
}
fn sort {|cb @argv| order &less-than=(less $cb) (-p $@argv) }

View File

@ -1,94 +0,0 @@
fn empty {|container| == (count $container) 0 }
fn values {|container| keys $container | each {|v| put $container[$v] } }
fn pvalues {|container| keys $container | peach {|v| put $container[$v] } }
fn value-of {|container k &default=$nil|
if (has-key $container $k) {
put $container[$k]
} else {
put $default
}
}
fn unzip {|container|
var lkeys lvalues = [] []
keys $container | each {|k|
set lkeys = [(all $lkeys) $k]
set lvalues = [(all $lvalues) $container[$k]]
}
put $lkeys $lvalues
}
fn zip {|lkeys lvalues|
var ck cv = (count $lkeys) (count $lvalues)
var c = $ck
if (> $ck $cv) {
set c = $cv
}
var result = [&]
range $c | peach {|i|
put [&k=$lkeys[$i] &i=$i]
} | peach {|e|
assoc $e v $lvalues[$e[i]]
} | each {|e|
set result[$e[k]] = $e[v]
}
put $result
}
fn to-map {|@argv|
use ./list
var container = (list:to-list $@argv)
zip [(range (count $container))] $container
}
fn to-set {|@argv|
use ./list
var m = [&]
each {|k| set m[$k] = $nil } (list:to-list $@argv)
put $m
}
fn mdissoc {|container @argv|
each {|k|
set container = (dissoc $container $k)
} $argv
put $container
}
fn massoc {|&replace=$true container @argv|
each {|e|
var k v = $@e
if (or $replace (not (has-key $container $k))) {
set container[$k] = $v
}
} $argv
put $container
}
fn add {|container key @values|
use ./list
var values = (list:to-list $values)
if (has-key $container $key) {
set container[$key] = [(all $container[$key]) $@values]
} else {
set container[$key] = $values
}
put $container
}
fn join {|container container2|
keys $container2 | each {|k|
set container[$k] = $container2[$k]
}
put $container
}
fn sorted-keys {|&reverse=$false &less-than=$nil container|
keys $container | order &reverse=$reverse &less-than=$less-than
}
fn sorted-values {|&reverse=$false &less-than=$nil container|
values $container | order &reverse=$reverse &less-than=$less-than
}

View File

@ -1,74 +0,0 @@
use re
use str
use ./list
fn is-moptions {|o| re:match '^\-\w{2,}$' $o }
fn is-soption {|o| re:match '^\-\w$' $o }
fn is-loption {|o| re:match '^\-\-\w+(=.*)?$' $o }
fn is-option {|o| or (is-soption $o) (is-loption $o) (is-moptions $o) }
fn -splitm {|o| each {|e| put -$e } $o[1:] }
fn -splitl {|o|
if (str:contains $o '=') {
var opt @value = (str:split '=' $o)
put $opt
str:join '=' $value
} else {
put $o
}
}
fn split {|o|
if (is-moptions $o) {
-splitm $o
} elif (is-loption $o) {
-splitl $o
} else {
put $o
}
}
fn -joins {|opt|
var o = (each {|o| put $o[1:] } $opt | str:join '')
if (not-eq $o '') { put -$o }
}
fn join {|@opt|
var opt = (list:-p $@opt)
var cb = {|o| or (is-soption $o) (is-moptions $o) }
var idx = [(list:filter-index-not $cb $opt)]
var i0 = 0
each {|i|
-joins $opt[{$i0}:$i]
put $opt[$i]
set i0 = (+ $i 1)
} $idx
-joins $opt[{$i0}:]
}
fn expand {|@argv| each $split~ (list:-p $@argv) }
fn map {|@argv|
use ./map
set argv = [(expand $@argv)]
var result i c = [&] 0 (count $argv)
while (< $i $c) {
var o = $argv[$i]
if (is-option $o) {
var k v = $o []
if (< $i (- $c 1)) {
var e = $argv[(+ $i 1)]
if (not (is-option $e)) {
set v = [$@v $e]
set i = (+ $i 1)
}
}
set result = (map:add $result $k $@v)
} else {
set result = (map:add $result '' [$o])
}
set i = (+ $i 1)
}
put $result
}

View File

@ -1,459 +0,0 @@
use path
use re
use str
fn is-pdf {|file|
eq (path:ext $file) '.pdf'
}
fn exist {|file|
or (path:is-dir &follow-symlink=$true $file) (path:is-regular &follow-symlink=$true $file)
}
fn not-exist {|file|
not (exist $file)
}
fn is-pdf-exist {|file|
and (is-pdf $file) (exist $file)
}
fn is-number {|v|
re:match '^\d+$' (to-string $v)
}
fn is-page-selection {|v|
re:match '^(r?\d+|z)(-(r?\d+|z))?(,(r?\d+|z)(-(r?\d+|z))?)*(:(even|odd))?$' (to-string $v)
}
fn is-rotate-selection {|v|
re:match '^(\+|-)?\d+(:(r?\d+|z)(-(r?\d+|z))?(,(r?\d+|z)(-(r?\d+|z))?)*(:(even|odd))?)?$' (to-string $v)
}
fn is-object {|v|
re:match '^ \d+\ 0\ R$' $v
}
fn is-selection {|v|
or (is-page-selection $v) (is-rotate-selection $v)
}
fn version {|in|
var version = (head -n 1 $in)
set version @_ = (str:split "\r" $version)
str:trim-prefix $version '%PDF-'
}
fn json {|in|
qpdf $in --json | from-json
}
fn form {|in|
var json = (json $in)
put $json[acroform]
}
fn encryption {|in|
var json = (json $in)
put $json[encrypt]
}
fn objects {|in|
var json = (json $in)
put $json[objects]
}
fn -format {|dict o|
var t = (kind-of $o)
if (eq $t string) {
if (has-key $dict $o) {
set o = $dict[$o]
}
} elif (eq $t list) {
set @o = (all $o | each {|e|
if (has-key $dict $e) {
put $dict[$e]
} else {
put $e
}
})
} elif (eq $t map) {
keys $o | each {|k|
set o[$k] = (-format $dict $o[$k])
}
}
put $o
}
fn -deep {|&dict=$nil objects @keys|
if (== (count $keys) 0) {
if (not-eq $dict $nil) {
put (-format $dict $objects) $true
} else {
put $objects $true
}
return
}
if (not-eq $dict $nil) {
set objects = (-format $dict $objects)
}
var id @next = $@keys
var t = (kind-of $objects)
if (eq $t map) {
if (has-key $objects $id) {
-deep &dict=$dict $objects[$id] $@next
}
} elif (eq $t list) {
if (and (is-number $id) (< $id (count $objects))) {
-deep &dict=$dict $objects[$id] $@next
}
} else {
put $nil $false
}
}
fn -object {|&extend=$false objects id|
var dict = $nil
if $extend {
set dict = $objects
}
var o _ = (-deep &dict=$dict $objects $id)
put $o
}
fn deep {|&extend=$false in @keys|
var dict = (objects $in)
if $extend {
-deep &dict=$dict $dict $@keys
} else {
-deep $dict $@keys
}
}
fn contains {|in @keys|
var _ ok = (deep $in $@keys)
put $ok
}
fn value {|&extend=$false in @keys|
var v _ = (deep &extend=$extend $in $@keys)
put $v
}
fn object {|&extend=$false in key|
-object &extend=$extend (objects $in) $key
}
fn -filter {|&extend=$false objects cond|
var out = [&]
keys $objects | each {|k|
var o = (-object &extend=$extend $objects $k)
if ($cond $o) {
set out[$k] = $o
}
}
put $out
}
fn filter {|&extend=$false in cond|
-filter &extend=$extend (objects $in) $cond
}
fn font-filter {|o|
and ^
(eq (kind-of $o) map) ^
(has-key $o /Type) ^
(eq $o[/Type] /Font)
}
fn fonts {|in|
var dict = (objects $in)
var fonts = (-filter $dict $font-filter~)
keys $fonts | each {|id|
var f = $fonts[$id]
keys $f | each {|k|
var v = $f[$k]
if (has-key $dict $v) {
set f[$k] = $dict[$v]
}
}
var fd = $f[/FontDescriptor]
keys $fd | each {|k|
var v = $fd[$k]
if (and (not-eq $k /FontFile2) (has-key $dict $v)) {
set fd[$k] = $dict[$v]
}
}
set f[/FontDescriptor] = $fd
set f[id] = $id
put $f
}
}
fn image-filter {|o|
and ^
(eq (kind-of $o) map) ^
(has-key $o /Subtype) ^
(eq $o[/Subtype] /Image)
}
fn images {|in|
var json = (json $in)
var dict = $json[objects]
all $json[pages] | each {|p|
var n = $p[pageposfrom1]
all $p[images] | each {|i|
var id = $i[object]
var img = (-object $dict $id)
keys $img | each {|k|
var v = $img[$k]
if (has-key $dict $v) {
set img[$k] = $dict[$v]
}
}
if (has-key $img /ColorSpace) {
var @cs = (all $img[/ColorSpace] | each {|v|
if (has-key $dict $v) {
put $dict[$v]
} else {
put $v
}
})
set img[/ColorSpace] = $cs
}
if (has-key $img /DecodeParms) {
var dp = $img[/DecodeParms]
keys $dp | each {|k|
var v = $dp[$k]
if (has-key $dict $v) {
set dp[$k] = $dict[$v]
}
}
set img[/DecodeParms] = $dp
}
set img[id] = $id
set img[page] = $n
set img[name] = $i[name]
put $img
}
}
}
fn trailer {|&extend=$false in|
object &extend=$extend $in 'trailer'
}
fn pages {|in|
var json = (json $in)
put $json[pages]
}
fn page {|in nb|
put (pages $in)[(- $nb 1)]
}
fn nb-pages {|in|
qpdf --show-npages $in
}
fn attachments {|in|
var data = []
var json = (json $in)
var attachments = $json[attachments]
if (> (count $attachments) 0) {
set @data = (qpdf --list-attachments $in | eawk {|_ f @_|
put $f
})
}
put $data
}
fn parse-selection {|@selection|
var out = []
var in = $nil
each {|e|
if (is-pdf $e) {
if (not-exist $e) {
fail (printf '%s: le fichier nexiste pas' $e)
}
if (not-eq $in $nil) {
set @out = $@out $in
}
set in = [
&file=$e
&rotate=$nil
&selections=[]
]
continue
}
if (eq $in $nil) {
fail (printf '%s: pas de fichier avant la sélection' $e)
}
var r rc = $in[rotate] $nil
if (is-page-selection $e) {
set rc = $false
} elif (is-rotate-selection $e) {
set rc = $true
} else {
fail (printf '%s: paramètre invalide' $e)
}
if (not-eq $r $rc) {
if (not-eq $r $nil) {
set @out = $@out $in
set in[selections] = []
}
set in[rotate] = $rc
}
set in[selections] = [ (all $in[selections]) $e ]
} $selection
if (not-eq $in $nil) {
set @out = $@out $in
}
put $out
}
fn -t {
mktemp -q /tmp/qpdf_XXXXXXXXXX.pdf
}
fn rotate {|&empty=$false &out=$nil &keep=$false in @rotate|
var @args = $in
if (eq $out $nil) {
set @args = $@args --replace-input
} else {
set @args = $@args $out
}
if $empty {
set @args = $@args --empty
}
if $keep {
set @rotate = (each {|r| put --rotate=$r } $rotate)
qpdf $@args $@rotate
return
}
var tmp = []
try {
var @tmp = (each {|r|
var t r @s = (-t) (str:split ':' $r)
set s = (str:join ':' $s)
if (eq $s '') {
qpdf $in $t --rotate=$r
} else {
qpdf $in $t --pages $in $s --
qpdf $t --replace-input --rotate=$r
}
put $t
} $rotate)
qpdf $@args --pages $@tmp --
} finally {
rm -f $@tmp
}
}
fn -catarg {|selection|
var f r s = $selection[file] $selection[rotate] $selection[selections]
var out tmp = [] []
if $r {
var t = (-t)
set @tmp = $t
set @out = $@out $t
rotate &out=$t $f $@s
} else {
set @out = $@out $f
if (> (count $s) 0) {
set @out = $@out (str:join ',' $s)
}
}
put $out $tmp
}
fn cat {|&empty=$false &collate=$nil &out=$nil @selection|
var inputs = (parse-selection $@selection)
var pages tmp = [] []
each {|in|
var a t = (-catarg $in)
set @pages = $@pages $@a
set @tmp = $@tmp $@t
} $inputs
try {
var in = $pages[0]
var @args = $in
if (eq $out $nil) {
set @args = $@args --replace-input
} else {
set @args = $@args $out
}
if $empty {
set @args = $@args --empty
}
if (bool $collate) {
var c = --collate
if (is-number $collate) {
set c = (str:join '=' [ $c $collate ])
}
set @args = $@args $c
}
qpdf $@args --pages $@pages --
} finally {
each {|t|
rm -f $t
} $tmp
}
}
fn decrypt {|&out=$nil in @passwd|
var @args = $in
if (eq $out $nil) {
set @args = $@args --replace-input
} else {
set @args = $@args $out
}
set @args = $@args --decrypt
if (> (count $passwd) 0) {
set @args = $@args $passwd[0]
}
qpdf $@args
}
fn -l0 {|n size|
set n = (to-string $n)
var l = (- $size (count $n))
str:join '' [ (repeat $l 0) $n ]
}
fn split {|&empty=$false &size=1 in out|
if (not-exist $out) {
mkdir $out
}
var args = []
if $empty {
set @args = --empty
}
qpdf $@args --split-pages=$size $in $out'/%d.pdf'
}
fn uncat {|&empty=$false in out @selection|
if (not-exist $out) {
mkdir $out
}
var i = 1
var n = (count $selection)
set n = (count (to-string $n))
each {|s|
cat &empty=$empty &out=$out'/'(-l0 $i $n)'.pdf' $in $s
set i = (+ $i 1)
} $selection
}
fn raw-stream {|in out id|
qpdf $in --show-object=$id --raw-stream-data > $out
}
fn filtered-stream {|in out id|
qpdf $in --show-object=$id --filtered-stream-data --normalize-content=n --stream-data=uncompress --decode-level=all > $out
}
fn attachment {|in out id|
qpdf $in --show-attachment=$id > $out
}

View File

@ -1,114 +0,0 @@
use str
use ./list
fn -repeat {|n s|
repeat $n $s | str:join ''
}
fn left {|size s|
var ss = (count $s)
var d = (- $size $ss)
if (<= $d 0) {
put $s[:$size]
} else {
put $s(-repeat $d ' ')
}
}
fn right {|size s|
var ss = (count $s)
var d = (- $size $ss)
if (<= $d 0) {
put $s[:$size]
} else {
put (-repeat $d ' ')$s
}
}
fn center {|size s|
use math
var ss = (count $s)
var d = (- $size $ss)
if (<= $d 0) {
var l = (math:ceil (/ -$d 2))
var r = (- (+ $ss $d) $l)
put $s[$l':'$r]
} else {
var l = (math:ceil (/ $d 2))
var r = (- $d $l)
put (-repeat $l ' ')$s(-repeat $r ' ')
}
}
fn -c2map {|col|
if (not-eq (kind-of $col) list) {
put []
return
}
list:loop {|i c|
var k = (kind-of $c)
var cf = [&min=0]
if (is $k string) {
set cf[min] = $c
} elif (is $k map) {
if (has-key $c min) {
set cf[min] = $c[min]
}
if (has-key $c align) {
set cf[align] = $c[align]
}
}
set col[$i] = $cf
} $col
put $col
}
fn -parsecols {|col &split=$false lst|
set col = (-c2map $col)
var cc lines = (count $col) $lst
if $split {
set lines = [ (eawk {|_ @args| put $args } $lst) ]
}
each {|l|
var cl = (count $l)
if (> $cl $cc) {
set col = [ $@col (range (- $cl $cc) | each {|_| put [&align=left &min=0] }) ]
set cc = $cl
}
list:loop {|i c|
var ccl = (+ (count $c) 1)
if (> $ccl $col[$i][min]) {
set col[$i][min] = $ccl
}
} $l
} $lines
put $col
}
fn -format {|f e|
var cb = $left~
if (has-key $f align) {
var a = $f[align]
if (is $a right) {
set cb = $right~
} elif (is $a center) {
set cb = $center~
}
}
$cb $f[min] $e
}
fn -formatline {|col line|
list:loop {|i v|
-format $col[$i] $v
} $line | str:join ''
}
fn format {|&col=[] &split=$false @lst|
var lines = (list:-p $@lst)
if $split {
set lines = [ (eawk {|l @args| put $args } $lines) ]
}
set col = (-parsecols $col $lines)
each {|l| -formatline $col $l } $lines
}

28
rc.elv
View File

@ -4,12 +4,12 @@ set-env EDITOR vim
set-env VIEW 'vim -R' set-env VIEW 'vim -R'
set-env CURRENT_SHELL 'elvish' set-env CURRENT_SHELL 'elvish'
set-env STARSHIP_CONFIG ~/.config/starship/starship.toml set-env STARSHIP_CONFIG ~/.config/starship/starship.toml
set paths = [$@paths /home/benjamin/bin /home/benjamin/Go/bin ] set @paths = $@paths /home/benjamin/bin /home/benjamin/Go/bin
{ {
use moi/util/list use framagit.org/benjamin.vaudour/elv-lib/mods/list
if (list:includes-not /usr/local/bin $paths) { if (list:contains-not /usr/local/bin $paths) {
set paths = [ $@paths /usr/local/bin ] set @paths = $@paths /usr/local/bin
} }
} }
@ -18,23 +18,27 @@ set notify-bg-job-success = $false
set edit:insert:binding[Tab] = { edit:completion:smart-start; edit:navigation:trigger-filter } set edit:insert:binding[Tab] = { edit:completion:smart-start; edit:navigation:trigger-filter }
set edit:insert:binding[Ctrl-H] = $edit:histlist:start~ set edit:insert:binding[Ctrl-H] = $edit:histlist:start~
set edit:prompt = { go-prompt } #set edit:prompt = { go-prompt }
set edit:rprompt = { nop } #set edit:rprompt = { nop }
# Starship prompt: https://starship.rs/
#eval (starship init elvish) #eval (starship init elvish)
eval (starship init elvish --print-full-init | upgrade-scripts-for-0.17 -lambda | slurp) eval (starship init elvish --print-full-init | upgrade-scripts-for-0.17 -lambda | slurp)
# Carapace completion: https://github.com/rsteube/carapace-bin # Carapace completion: https://github.com/rsteube/carapace-bin
eval (carapace _carapace|slurp) eval (carapace _carapace|slurp)
# Initialisation des aliases et de lautocomplétion
use framagit.org/benjamin.vaudour/elv-lib/alias
use framagit.org/benjamin.vaudour/elv-lib/completion
# On ajoute les modules natifs que jutilise beaucoup # On ajoute les modules natifs que jutilise beaucoup
use math
use str use str
use re use re
use math #use framagit.org/benjamin.vaudour/elv-lib/mods/format
#use framagit.org/benjamin.vaudour/elv-lib/mods/list
use moi/completion #use framagit.org/benjamin.vaudour/elv-lib/mods/map
use moi/alias
use moi/util/common
use moi/util/format
{ {
use ./aliases/get-uid uid use ./aliases/get-uid uid