From 87eb82a791b6b03f7a52d996571ce5216a39a2c3 Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR Date: Sat, 11 Dec 2021 21:09:02 +0000 Subject: [PATCH] =?UTF-8?q?Compatibilit=C3=A9=20elvish=200.17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + aliases/addbadips.elv | 4 +- aliases/adguard.elv | 2 +- aliases/aliases.elv | 24 +++--- aliases/arc.elv | 12 +-- aliases/carapace.elv | 2 +- aliases/checklinks.elv | 14 ++-- aliases/cleankh.elv | 12 +-- aliases/commitkcp.elv | 4 +- aliases/digall.elv | 30 ++++---- aliases/get-uid.elv | 18 ++--- aliases/ipinfo.elv | 6 +- aliases/ipof.elv | 26 +++---- aliases/ls.elv | 12 +-- aliases/meteo.elv | 2 +- aliases/pacmanhisto.elv | 2 +- aliases/pdf2grey.elv | 10 +-- aliases/publicip.elv | 6 +- aliases/random.elv | 26 +++---- aliases/riot.elv | 2 +- aliases/sendsms.elv | 2 +- aliases/translate.elv | 4 +- aliases/upd.elv | 2 +- aliases/vi.elv | 16 ++-- aliases/vivaldiprofile.elv | 14 ++-- aliases/youtube-dl.elv | 12 +-- lib/moi/completion/arc.elv | 8 +- lib/moi/completion/archiver.elv | 4 +- lib/moi/completion/desarchiver.elv | 2 +- lib/moi/completion/kcp.elv | 2 +- lib/moi/completion/mpv.elv | 4 +- lib/moi/completion/pacman.elv | 16 ++-- lib/moi/completion/ssh.elv | 20 ++--- lib/moi/completion/sudo.elv | 2 +- lib/moi/completion/use.elv | 6 +- lib/moi/util/common.elv | 22 +++--- lib/moi/util/condition.elv | 6 +- lib/moi/util/file.elv | 32 ++++---- lib/moi/util/ip.elv | 34 ++++----- lib/moi/util/list.elv | 116 ++++++++++++++--------------- lib/moi/util/map.elv | 44 +++++------ lib/moi/util/option.elv | 28 +++---- lib/moi/util/strutil.elv | 34 ++++----- 43 files changed, 323 insertions(+), 322 deletions(-) diff --git a/.gitignore b/.gitignore index 285352f..25f1436 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +*.old lib/github.com private diff --git a/aliases/addbadips.elv b/aliases/addbadips.elv index b4aa6aa..8d969de 100644 --- a/aliases/addbadips.elv +++ b/aliases/addbadips.elv @@ -1,4 +1,4 @@ -edit:add-var addbadips~ [@argv]{ +edit:add-var addbadips~ {|@argv| use moi/util/ip use moi/util/list use moi/util/map @@ -7,7 +7,7 @@ edit:add-var addbadips~ [@argv]{ var f = ~/bad_ips.txt var ips = [ (cat $f) ] var m c = (map:to-set $ips) 0 - each [e]{ + each {|e| var cond = (and (not (has-key $m $e)) (ip:is-ipv4 $e)) if $cond { echo $e ajoutée diff --git a/aliases/adguard.elv b/aliases/adguard.elv index b11ecb9..9a8155e 100644 --- a/aliases/adguard.elv +++ b/aliases/adguard.elv @@ -1,4 +1,4 @@ -edit:add-var adguard~ [@argv]{ +edit:add-var adguard~ {|@argv| use str sudo chattr -i /etc/resolv.conf var beg = 's|^.*\(192\.168\.0\.25\)|' diff --git a/aliases/aliases.elv b/aliases/aliases.elv index 57733d4..a290f80 100644 --- a/aliases/aliases.elv +++ b/aliases/aliases.elv @@ -1,17 +1,17 @@ -edit:add-var cat~ [@argv]{ e:bat -pp --tabs 2 $@argv } -edit:add-var cp~ [@argv]{ e:cp -iv $@argv } +edit:add-var cat~ {|@argv| e:bat -pp --tabs 2 $@argv } +edit:add-var cp~ {|@argv| e:cp -iv $@argv } edit:add-var curl~ $e:curlie~ edit:add-var df~ $e:duf~ edit:add-var diff~ $e:colordiff~ -edit:add-var dig~ [@argv]{ e:dig +noall +answer $@argv } -edit:add-var dogall~ [@argv]{ e:dog A AAAA CNAME TXT MX NS SOA $@argv } -edit:add-var du~ [@argv]{ e:du -h $@argv } +edit:add-var dig~ {|@argv| e:dig +noall +answer $@argv } +edit:add-var dogall~ {|@argv| e:dog A AAAA CNAME TXT MX NS SOA $@argv } +edit:add-var du~ {|@argv| e:du -h $@argv } edit:add-var git~ $e:hub~ -edit:add-var grep~ [@argv]{ e:grep --color=tty -d skip $@argv } -edit:add-var mv~ [@argv]{ e:mv -iv $@argv } -edit:add-var pf~ [@argv]{ e:pkgfile -vri $@argv } -edit:add-var rm~ [@argv]{ e:rm -Iv $@argv } +edit:add-var grep~ {|@argv| e:grep --color=tty -d skip $@argv } +edit:add-var mv~ {|@argv| e:mv -iv $@argv } +edit:add-var pf~ {|@argv| e:pkgfile -vri $@argv } +edit:add-var rm~ {|@argv| e:rm -Iv $@argv } edit:add-var rename~ $e:go-rename~ -edit:add-var timidity~ [@argv]{ e:timidity -a -K 1 $@argv } -edit:add-var tree~ [@argv]{ e:tree -puhC --dirsfirst --du -D $@argv } -edit:add-var wget~ [@argv]{ e:wget2 $@argv } +edit:add-var timidity~ {|@argv| e:timidity -a -K 1 $@argv } +edit:add-var tree~ {|@argv| e:tree -puhC --dirsfirst --du -D $@argv } +edit:add-var wget~ {|@argv| e:wget2 $@argv } diff --git a/aliases/arc.elv b/aliases/arc.elv index 611c807..9e51066 100644 --- a/aliases/arc.elv +++ b/aliases/arc.elv @@ -1,6 +1,6 @@ var cmd = $e:arc~ -edit:add-var archiver~ [@argv]{ +edit:add-var archiver~ {|@argv| use str var help = { echo "Usage : archiver [(-zlxgb)] " @@ -27,7 +27,7 @@ edit:add-var archiver~ [@argv]{ if (== (count $argv) 0) { $help } - peach [f]{ + peach {|f| var ff = $f if (eq $ff[-1] /) { set f = $ff[..-1] @@ -37,16 +37,16 @@ edit:add-var archiver~ [@argv]{ } $argv } -edit:add-var desarchiver~ [@argv]{ - peach [f]{ +edit:add-var desarchiver~ {|@argv| + peach {|f| var ff = $f $cmd unarchive $ff echo "'"$ff"'" désarchivé } $argv } -edit:add-var listarchiver~ [@argv]{ - peach [f]{ +edit:add-var listarchiver~ {|@argv| + peach {|f| var ff = $f $cmd ls $ff } $argv diff --git a/aliases/carapace.elv b/aliases/carapace.elv index 665458f..f31bbae 100644 --- a/aliases/carapace.elv +++ b/aliases/carapace.elv @@ -1,4 +1,4 @@ -edit:add-var updatecarapace~ []{ +edit:add-var updatecarapace~ {|| var cur = (pwd) var wd = $E:HOME/Documents/OtherGo/github.com/rsteube/carapace-bin var bd = $E:HOME/Go/bin diff --git a/aliases/checklinks.elv b/aliases/checklinks.elv index edacd04..13b9367 100644 --- a/aliases/checklinks.elv +++ b/aliases/checklinks.elv @@ -1,8 +1,8 @@ use str -fn -check [f]{ +fn -check {|f| var @missings = (try { - sudo ldd $f 2>/dev/null | grep 'not found' | eawk [_ m @_]{ put $m } + sudo ldd $f 2>/dev/null | grep 'not found' | eawk {|_ m @_| put $m } } except e { }) if (> (count $missings) 1) { @@ -11,28 +11,28 @@ fn -check [f]{ } } -fn -check-executable [f]{ +fn -check-executable {|f| if ?(test -x $f) { -check $f } } -fn -cbin []{ +fn -cbin {|| echo (styled 'Searching broken binaries…' bold bright-yellow) sudo find /usr/bin -maxdepth 1 -type f 2>/dev/null | each $-check~ } -fn -clib []{ +fn -clib {|| echo (styled 'Searching broken libs…' bold bright-yellow) sudo find /usr/lib/*.so* -maxdepth 1 -type f 2>/dev/null | each $-check-executable~ } -fn -copt []{ +fn -copt {|| echo (styled 'Searching broken binariess in opt…' bold bright-yellow) sudo find /opt -maxdepth 3 -type f 2>/dev/null | each $-check-executable~ } -edit:add-var checklinks~ []{ +edit:add-var checklinks~ {|| -cbin -clib -copt diff --git a/aliases/cleankh.elv b/aliases/cleankh.elv index 588be69..50c83cd 100644 --- a/aliases/cleankh.elv +++ b/aliases/cleankh.elv @@ -1,10 +1,10 @@ -edit:add-var cleankh~ [@argv]{ +edit:add-var cleankh~ {|@argv| use str use moi/util/condition use moi/util/list use moi/util/ip - fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } - fn sort-host [e1 e2]{ + fn less {|cmp e1 e2| < ($cmp $e1 $e2) 0 } + fn sort-host {|e1 e2| var i1 = (condition:cset (ip:is-ip $e1) 1 0) var i2 = (condition:cset (ip:is-ip $e2) 1 0) if (!= $i1 $i2) { @@ -17,17 +17,17 @@ edit:add-var cleankh~ [@argv]{ } var khosts = [&] - cat ~/.ssh/known_hosts | eawk [_ hosts @rest]{ + cat ~/.ssh/known_hosts | eawk {|_ hosts @rest| var key = (str:join ' ' $rest) if (not (has-key $khosts $key)) { set khosts[$key] = [&] } - str:split , $hosts | each [h]{ + str:split , $hosts | each {|h| set khosts[$key][$h] = $nil } } - var lines = [(keys $khosts | each [key]{ + var lines = [(keys $khosts | each {|key| var hosts = (keys $khosts[$key] | order &less-than=$sort-host~ | str:join ,) put $hosts $key | str:join ' ' })] diff --git a/aliases/commitkcp.elv b/aliases/commitkcp.elv index b5403f3..634dd8d 100644 --- a/aliases/commitkcp.elv +++ b/aliases/commitkcp.elv @@ -1,11 +1,11 @@ -edit:add-var commitkcp~ [@argv]{ +edit:add-var commitkcp~ {|@argv| use str var m = [ &name= &ver= ] - keys $m | each [k]{ + keys $m | each {|k| var p = 'pkg'$k'=' set m[$k] = (str:trim-prefix (cat PKGBUILD | grep $p | take 1) $p) } diff --git a/aliases/digall.elv b/aliases/digall.elv index 646877b..15aef3b 100644 --- a/aliases/digall.elv +++ b/aliases/digall.elv @@ -1,4 +1,4 @@ -edit:add-var digall~ [@argv]{ +edit:add-var digall~ {|@argv| use moi/util/list use moi/util/strutil use str @@ -27,12 +27,12 @@ edit:add-var digall~ [@argv]{ ] var domains use_dig = [] $true - fn init []{ + fn init {|| if (and (> (count $argv) 0) (is $argv[0] -d)) { set use_dig = $false set argv = $argv[1:] } - each [e]{ + each {|e| if (is $e[0] '+') { set flags = [ (all $flags) $e ] } elif (is $e[0] @) { @@ -48,10 +48,10 @@ edit:add-var digall~ [@argv]{ } } - fn format [results]{ - eawk [_ @args]{ + fn format {|results| + eawk {|_ @args| put [ $args[0] (all $args[2..]) ] - } $results | list:remove-duplicate | list:sort [l1 l2]{ + } $results | list:remove-duplicate | list:sort {|l1 l2| var c = (str:compare $l1[0] $l2[0]) if (== $c 0) { set c = (str:compare $l1[2] $l2[2]) @@ -59,23 +59,23 @@ edit:add-var digall~ [@argv]{ } | strutil:format &col=$cols | str:join "\n" } - fn drillr [d @args]{ - var results = [ (peach [r]{ - e:drill $@args $r $d | list:filter [e]{ + fn drillr {|d @args| + var results = [ (peach {|r| + e:drill $@args $r $d | list:filter {|e| and (> (count $e) 2) (not (is $e[:2] ';;')) } } $records) ] format $results } - fn digr [d @args]{ - var results = [ (peach [r]{ + fn digr {|d @args| + var results = [ (peach {|r| e:dig $@args $r $d $@flags } $records) ] format $results } - fn req [d @args]{ + fn req {|d @args| if $use_dig { digr $d $@args } else { @@ -83,15 +83,15 @@ edit:add-var digall~ [@argv]{ } } - fn digs [s d]{ + fn digs {|s d| put (echo (styled $s bright-yellow)) req $d $s } - fn digd [d]{ + fn digd {|d| var results = [ (echo (styled 'Domaine: '$d bright-green)) ] if (> (count $servers) 0) { - each [s]{ + each {|s| set @results = $@results (digs $s $d) } $servers } else { diff --git a/aliases/get-uid.elv b/aliases/get-uid.elv index fac9a08..7a963b9 100644 --- a/aliases/get-uid.elv +++ b/aliases/get-uid.elv @@ -1,7 +1,7 @@ use re use str -fn -value [v]{ +fn -value {|v| var p = (re:find '^(\d+)\((\w+)\)$' $v) if (and (has-key $p groups) (== (count $p[groups]) 3)) { put [ @@ -11,7 +11,7 @@ fn -value [v]{ } } -fn -values [v]{ +fn -values {|v| var vv = [ (str:split ',' $v) ] if (== (count $vv) 1) { -value $vv[0] @@ -20,9 +20,9 @@ fn -values [v]{ } } -fn -get-id []{ +fn -get-id {|| var id = [&] - str:split ' ' (id) | each [e]{ + str:split ' ' (id) | each {|e| var p = [ (str:split '=' $e) ] if (== (count $p) 2) { set id[$p[0]] = (-values $p[1]) @@ -31,19 +31,19 @@ fn -get-id []{ put $id } -fn -get [k]{ +fn -get {|k| var id = (-get-id) if (has-key $id $k) { put $id[$k] } } -fn -is-root []{ +fn -is-root {|| == (-get uid)[number] 0 } edit:add-var get-id~ $-get-id~ -edit:add-var get-uid~ []{ -get uid } -edit:add-var get-gid~ []{ -get gid } -edit:add-var get-groups~ []{ -get groups; -get groupes } +edit:add-var get-uid~ {|| -get uid } +edit:add-var get-gid~ {|| -get gid } +edit:add-var get-groups~ {|| -get groups; -get groupes } edit:add-var is-root~ $-is-root~ diff --git a/aliases/ipinfo.elv b/aliases/ipinfo.elv index 4757ec7..d12e355 100644 --- a/aliases/ipinfo.elv +++ b/aliases/ipinfo.elv @@ -1,14 +1,14 @@ -edit:add-var ipinfo~ [@argv]{ +edit:add-var ipinfo~ {|@argv| use moi/util/ip use str var token = (cat $E:HOME/.config/elvish/private/ipinfo_token) - fn url-of [ip]{ + fn url-of {|ip| str:join '' [ 'ipinfo.io/' $ip '?token=' $token] } - fn info [ip]{ + fn info {|ip| if (ip:is-ip $ip) { var url = (url-of $ip) echo 'URL: '$url diff --git a/aliases/ipof.elv b/aliases/ipof.elv index 4256687..9f966e9 100644 --- a/aliases/ipof.elv +++ b/aliases/ipof.elv @@ -1,11 +1,11 @@ -edit:add-var ipof~ [@argv]{ +edit:add-var ipof~ {|@argv| use str use moi/util/condition use moi/util/ip - fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } + fn less {|cmp e1 e2| < ($cmp $e1 $e2) 0 } - fn sort-ip [e1 e2]{ + fn sort-ip {|e1 e2| var i1 = (condition:cset (ip:is-ipv4 $e1) 0 (condition:cset (ip:is-ipv6 $e1) 1 2)) var i2 = (condition:cset (ip:is-ipv4 $e2) 0 (condition:cset (ip:is-ipv6 $e2) 1 2)) if (!= $i1 $i2) { @@ -19,18 +19,18 @@ edit:add-var ipof~ [@argv]{ } } - fn dnssolve [d]{ + fn dnssolve {|d| var result = [ &ips=[&] &resolvers='' &txt='' ] - e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach [l]{ + e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach {|l| var ll = $l var idx = (+ (str:index $ll :) 1) var value = (str:trim-space $ll[$idx".."]) if (or (str:has-prefix $ll 'IPv4 addresses') (str:has-prefix $ll 'IPv6 addresses')) { - str:split ', ' $value | peach [e]{ + str:split ', ' $value | peach {|e| var ee = $e if (ip:is-ip $ee) { set result[ips][$ee] = $nil @@ -48,13 +48,13 @@ edit:add-var ipof~ [@argv]{ } var hosts = [&] - cat /etc/hosts | peach [l]{ + cat /etc/hosts | peach {|l| var ll = (str:trim-space $l) if (and (not-eq $ll '') (not-eq $ll[0] '#')) { put $ll } - } | eawk [_ ip @domains]{ - peach [d]{ + } | eawk {|_ ip @domains| + peach {|d| var dd = $d if (has-key $hosts $dd) { set hosts[$dd][$ip] = $nil @@ -65,7 +65,7 @@ edit:add-var ipof~ [@argv]{ } var remote = [&] - peach [d]{ + peach {|d| var dd = $d var solve = (dnssolve $dd) if (> (keys $solve[ips] | count) 0) { @@ -73,14 +73,14 @@ edit:add-var ipof~ [@argv]{ } } $argv - each [d]{ + each {|d| var exists = $false echo (styled 'Resolving '$d'…' bright-green) echo if (has-key $hosts $d) { set exists = $true echo (styled ' local:' bright-yellow) - keys $hosts[$d] | order &less-than=$sort-ip~ | each [ip]{ + keys $hosts[$d] | order &less-than=$sort-ip~ | each {|ip| echo ' - '$ip } } @@ -89,7 +89,7 @@ edit:add-var ipof~ [@argv]{ var info = $remote[$d] echo (styled ' remote:' bright-yellow) echo ' IPs:' - keys $info[ips] | order &less-than=$sort-ip~ | each [ip]{ + keys $info[ips] | order &less-than=$sort-ip~ | each {|ip| echo ' - '$ip } echo ' Resolvers: '$info[resolvers] diff --git a/aliases/ls.elv b/aliases/ls.elv index f0ef629..e9f8235 100644 --- a/aliases/ls.elv +++ b/aliases/ls.elv @@ -1,14 +1,14 @@ -fn -ls [@argv]{ +fn -ls {|@argv| e:ls -N --color=auto --group-directories-first --ignore=lost+found '--time-style=+"%Y.%m.%d %H:%M"' --indicator-style=classify $@argv } -fn -go-ls [@argv]{ +fn -go-ls {|@argv| e:go-ls --group-directories-first -T '+%Y.%m.%d %H:%M' --git $@argv } var cmd = $-go-ls~ -edit:add-var ls~ [@argv]{ $cmd $@argv } -edit:add-var la~ [@argv]{ $cmd -a $@argv } -edit:add-var ll~ [@argv]{ $cmd -lh $@argv } -edit:add-var lla~ [@argv]{ $cmd -lha $@argv } +edit:add-var ls~ {|@argv| $cmd $@argv } +edit:add-var la~ {|@argv| $cmd -a $@argv } +edit:add-var ll~ {|@argv| $cmd -lh $@argv } +edit:add-var lla~ {|@argv| $cmd -lha $@argv } diff --git a/aliases/meteo.elv b/aliases/meteo.elv index e5cd72e..d80e9bf 100644 --- a/aliases/meteo.elv +++ b/aliases/meteo.elv @@ -1,4 +1,4 @@ -edit:add-var meteo~ [@argv]{ +edit:add-var meteo~ {|@argv| use str #curl --connect-timeout 2 "wttr.in/"(joins '' $_args)"?lang=fr" e:curlie -s --connect-timeout 2 "wttr.in/"(str:join '' $argv)"?lang=fr" diff --git a/aliases/pacmanhisto.elv b/aliases/pacmanhisto.elv index 4a3f29b..2347e18 100644 --- a/aliases/pacmanhisto.elv +++ b/aliases/pacmanhisto.elv @@ -1 +1 @@ -edit:add-var pacmanhisto~ [@argv]{ e:cat /var/log/pacman.log | e:grep "ALPM" | e:grep $@argv } +edit:add-var pacmanhisto~ {|@argv| e:cat /var/log/pacman.log | e:grep "ALPM" | e:grep $@argv } diff --git a/aliases/pdf2grey.elv b/aliases/pdf2grey.elv index dc63cae..d62dcac 100644 --- a/aliases/pdf2grey.elv +++ b/aliases/pdf2grey.elv @@ -1,4 +1,4 @@ -edit:add-var pdf2grey~ [@argv]{ +edit:add-var pdf2grey~ {|@argv| use re use str var m = [ @@ -6,7 +6,7 @@ edit:add-var pdf2grey~ [@argv]{ &split=50 ] - fn _help []{ + fn _help {|| echo 'Usage: pdf2gray [-hn ()] .pdf .pdf' echo '' echo 'Options:' @@ -14,15 +14,15 @@ edit:add-var pdf2grey~ [@argv]{ echo ' -n [0-100] Convertit en noir et blanc. Si un nombre est spécifié, indique le pourcentage de coupure (par défaut: 50)' } - fn is_pdf [e]{ + fn is_pdf {|e| re:match '.*\.pdf$' $e } - fn is_int [e]{ + fn is_int {|e| re:match '^\d+$' $e } - fn is_split [e]{ + fn is_split {|e| and (>= $e 0) (<= $e 100) } diff --git a/aliases/publicip.elv b/aliases/publicip.elv index 66c5148..a37ee36 100644 --- a/aliases/publicip.elv +++ b/aliases/publicip.elv @@ -1,9 +1,9 @@ -edit:add-var publicip~ [@argv]{ - var f = [t]{ +edit:add-var publicip~ {|@argv| + var f = {|t| use moi/util/condition set t = (condition:cset (eq $t -6) -6 -4) var l = (condition:cset (eq $t -6) 'IPv6: ' 'IPv4: ') - curl $t ifconfig.co/ip 2> /dev/null | each [ip]{ echo (styled $l bold yellow) $ip } + curl $t ifconfig.co/ip 2> /dev/null | each {|ip| echo (styled $l bold yellow) $ip } } $f -4 $f -6 diff --git a/aliases/random.elv b/aliases/random.elv index 18bc5d6..1ac3063 100644 --- a/aliases/random.elv +++ b/aliases/random.elv @@ -1,44 +1,44 @@ use str use re -fn -bytes [begin size]{ +fn -bytes {|begin size| str:from-codepoints (range $begin (+ $begin $size)) } -fn -upper []{ +fn -upper {|| -bytes 0x41 26 } -fn -lower []{ +fn -lower {|| -bytes 0x61 26 } -fn -digit []{ +fn -digit {|| -bytes 0x30 10 } -fn -j [@strings]{ +fn -j {|@strings| str:join '' $strings } -fn -alpha []{ +fn -alpha {|| -j (-upper) (-lower) } -fn -alphanum []{ +fn -alphanum {|| -j (-alpha) (-digit) } -fn -random [chars]{ +fn -random {|chars| var size = (count $chars) print $chars[(randint 0 $size)] } -fn -is-int [arg]{ +fn -is-int {|arg| re:match '^\d+$' $arg } -fn -help []{ +fn -help {|| echo 'Génère une chaîne de caractères aléatoires' echo 'Usage: genstr [(-auldh)] [] []' echo '' @@ -54,9 +54,9 @@ fn -help []{ echo 'Si aucune classe de caractères ou liste de caractères n’est fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).' } -edit:add-var genstr~ [@args]{ +edit:add-var genstr~ {|@args| var len chars h = 0 '' $false - each [e]{ + each {|e| if (or (eq $e -h) (eq $e --help)) { set h = $true -help @@ -86,7 +86,7 @@ edit:add-var genstr~ [@args]{ set chars = [(str:split '' $chars)] - range $len | each [_]{ + range $len | each {|_| -random $chars } echo diff --git a/aliases/riot.elv b/aliases/riot.elv index 21df6c9..ab1e83e 100644 --- a/aliases/riot.elv +++ b/aliases/riot.elv @@ -1,3 +1,3 @@ -edit:add-var riot~ []{ +edit:add-var riot~ {|| cat $E:HOME/.config/elvish/private/riot.txt | xclip -selection clipboard } diff --git a/aliases/sendsms.elv b/aliases/sendsms.elv index 23f47cd..6e1ba23 100644 --- a/aliases/sendsms.elv +++ b/aliases/sendsms.elv @@ -1,4 +1,4 @@ -edit:add-var sendsms~ [@argv]{ +edit:add-var sendsms~ {|@argv| use str var api = https://smsapi.free-mobile.fr/sendmsg diff --git a/aliases/translate.elv b/aliases/translate.elv index 6b3d8eb..b64214d 100644 --- a/aliases/translate.elv +++ b/aliases/translate.elv @@ -1,2 +1,2 @@ -edit:add-var trsen~ [@argv]{ e:rlwrap trans -b -t en $@argv } -edit:add-var trsfr~ [@argv]{ e:rlwrap trans -b -t fr $@argv } +edit:add-var trsen~ {|@argv| e:rlwrap trans -b -t en $@argv } +edit:add-var trsfr~ {|@argv| e:rlwrap trans -b -t fr $@argv } diff --git a/aliases/upd.elv b/aliases/upd.elv index 8cdc85a..2e4fa89 100644 --- a/aliases/upd.elv +++ b/aliases/upd.elv @@ -1,4 +1,4 @@ -edit:add-var upd~ [@args]{ +edit:add-var upd~ {|@args| if ?(mirror-check --fast) { sudo pacman -Syu } diff --git a/aliases/vi.elv b/aliases/vi.elv index b9f6920..b9e6ecf 100644 --- a/aliases/vi.elv +++ b/aliases/vi.elv @@ -1,10 +1,10 @@ var cmd = $e:vim~ -edit:add-var vi~ [@argv]{ $cmd $@argv } -edit:add-var vb~ [@argv]{ $cmd $@argv ~/.bashrc } -edit:add-var vc~ [@argv]{ $cmd $@argv ~/.vimrc } -edit:add-var ve~ [@argv]{ $cmd $@argv ~/.config/elvish/rc.elv } -edit:add-var vf~ [@argv]{ $cmd $@argv ~/.config/fish/config.fish } -edit:add-var vp~ [@argv]{ $cmd $@argv PKGBUILD } -edit:add-var vv~ [@argv]{ $cmd -R $@argv } -edit:add-var vz~ [@argv]{ $cmd $@argv ~/.zshrc } +edit:add-var vi~ {|@argv| $cmd $@argv } +edit:add-var vb~ {|@argv| $cmd $@argv ~/.bashrc } +edit:add-var vc~ {|@argv| $cmd $@argv ~/.vimrc } +edit:add-var ve~ {|@argv| $cmd $@argv ~/.config/elvish/rc.elv } +edit:add-var vf~ {|@argv| $cmd $@argv ~/.config/fish/config.fish } +edit:add-var vp~ {|@argv| $cmd $@argv PKGBUILD } +edit:add-var vv~ {|@argv| $cmd -R $@argv } +edit:add-var vz~ {|@argv| $cmd $@argv ~/.zshrc } diff --git a/aliases/vivaldiprofile.elv b/aliases/vivaldiprofile.elv index 9c424a5..d7a8bf5 100644 --- a/aliases/vivaldiprofile.elv +++ b/aliases/vivaldiprofile.elv @@ -1,16 +1,16 @@ -fn -sync [d1 d2]{ +fn -sync {|d1 d2| rsync -azh --delete --info=progress2,stats ~/.config/$d1/ ~/.config/$d2/ } -fn -save [d]{ +fn -save {|d| -sync $d $d.save } -fn -restore [d]{ +fn -restore {|d| -sync $d.save $d } -edit:add-var viv-save~ []{ -save vivaldi } -edit:add-var viv-restore~ []{ -restore vivaldi } -edit:add-var vivd-save~ []{ -save vivaldi-developer } -edit:add-var vivd-restore~ []{ -restore vivaldi-developer } +edit:add-var viv-save~ {|| -save vivaldi } +edit:add-var viv-restore~ {|| -restore vivaldi } +edit:add-var vivd-save~ {|| -save vivaldi-developer } +edit:add-var vivd-restore~ {|| -restore vivaldi-developer } diff --git a/aliases/youtube-dl.elv b/aliases/youtube-dl.elv index 54b87ad..1f460c0 100644 --- a/aliases/youtube-dl.elv +++ b/aliases/youtube-dl.elv @@ -1,16 +1,16 @@ var cmd = $e:yt-dlp~ #var cmd = $e:youtube-dl~ -fn -dl [@argv]{ +fn -dl {|@argv| $cmd --no-check-certificate $@argv } -fn -audio [@argv]{ +fn -audio {|@argv| -dl -x --audio-format best $@argv } -edit:add-var youtube-dl~ [@argv]{ -dl $@argv } -edit:add-var youtube-audio~ [@argv]{ -audio $@argv } -edit:add-var yt-dl~ [@argv]{ -dl $@argv } -edit:add-var yt-audio~ [@argv]{ -audio $@argv } +edit:add-var youtube-dl~ {|@argv| -dl $@argv } +edit:add-var youtube-audio~ {|@argv| -audio $@argv } +edit:add-var yt-dl~ {|@argv| -dl $@argv } +edit:add-var yt-audio~ {|@argv| -audio $@argv } diff --git a/lib/moi/completion/arc.elv b/lib/moi/completion/arc.elv index cfdb4b0..b72f5b0 100644 --- a/lib/moi/completion/arc.elv +++ b/lib/moi/completion/arc.elv @@ -13,20 +13,20 @@ var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ] fn -comp-commands { all $commands } -fn -comp-archive-files [m]{ +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]{ +fn -comp-inline-files {|archive| try { - arc ls $archive | eawk [_ @argv]{ put $argv[-1] } + arc ls $archive | eawk {|_ @argv| put $argv[-1] } } except e { nop } } -fn complete [@argv]{ +fn complete {|@argv| var c cmd = (count $argv) $argv[1] if (== $c 2) { -comp-commands diff --git a/lib/moi/completion/archiver.elv b/lib/moi/completion/archiver.elv index b536d21..4722adc 100644 --- a/lib/moi/completion/archiver.elv +++ b/lib/moi/completion/archiver.elv @@ -17,12 +17,12 @@ var description = [ ] fn -options { - each [o]{ + each {|o| put [&short=$o &desc=$description[$o]] } $options } -fn complete [@argv]{ +fn complete {|@argv| edit:complete-getopt $argv [(-options)] [ $edit:complete-filename~ ...] } diff --git a/lib/moi/completion/desarchiver.elv b/lib/moi/completion/desarchiver.elv index 08fec76..ce879f9 100644 --- a/lib/moi/completion/desarchiver.elv +++ b/lib/moi/completion/desarchiver.elv @@ -3,7 +3,7 @@ use moi/util/file var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ] -fn complete [@argv]{ +fn complete {|@argv| var m = $argv[-1] var type = (condition:cset (eq $m '') prefix deep-prefix) file:match-extensions &type=$type $m $@extensions diff --git a/lib/moi/completion/kcp.elv b/lib/moi/completion/kcp.elv index 68819e3..8a8da0b 100644 --- a/lib/moi/completion/kcp.elv +++ b/lib/moi/completion/kcp.elv @@ -29,7 +29,7 @@ var np = [ -V ] -fn complete [@argv]{ +fn complete {|@argv| var c = (count $argv) if (== $c 2) { all $options diff --git a/lib/moi/completion/mpv.elv b/lib/moi/completion/mpv.elv index 4ebd30b..6952fe3 100644 --- a/lib/moi/completion/mpv.elv +++ b/lib/moi/completion/mpv.elv @@ -35,12 +35,12 @@ var extensions = [ wmx ] -fn -files [m]{ +fn -files {|m| var type = (condition:cset (eq $m '') prefix deep-prefix) file:match-extensions &type=$type $m $@extensions } -fn complete [@argv]{ +fn complete {|@argv| var c = (count $argv) if (== $c 2) { put --speed diff --git a/lib/moi/completion/pacman.elv b/lib/moi/completion/pacman.elv index 74b1016..7598386 100644 --- a/lib/moi/completion/pacman.elv +++ b/lib/moi/completion/pacman.elv @@ -2,31 +2,31 @@ use moi/util/condition use moi/util/file use moi/util/list -fn -local-packages { pacman -Q | eawk [_ p @_]{ put $p } } +fn -local-packages { pacman -Q | eawk {|_ p @_| put $p } } fn -repo-packages { - var packages = [(pacman -Ss | list:ploop &step=2 [_ v]{ + var packages = [(pacman -Ss | list:ploop &step=2 {|_ v| put $v - } | eawk [_ p @_]{ + } | eawk {|_ p @_| put $p })] var spackages = [&] use str - peach [p]{ + peach {|p| str:split '/' $p - } $packages | peach [e]{ + } $packages | peach {|e| set spackages[$e] = $nil } keys $spackages all $packages } -fn -downloaded-packages [m]{ +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 } +fn -local-file {|m| edit:complete-filename $m } var options = [ -h @@ -81,7 +81,7 @@ var fpack = [ -Qo ] -fn complete [@argv]{ +fn complete {|@argv| var c = (count $argv) if (< $c 3) { all $options diff --git a/lib/moi/completion/ssh.elv b/lib/moi/completion/ssh.elv index 5d08712..3d8619c 100644 --- a/lib/moi/completion/ssh.elv +++ b/lib/moi/completion/ssh.elv @@ -16,15 +16,15 @@ var names = [ ] fn -kh { - cat ~/.ssh/known_hosts | peach [l]{ + cat ~/.ssh/known_hosts | peach {|l| put [(str:split ' ' $l)] - } | peach [e]{ + } | peach {|e| var domains @_ = $@e str:split ',' $domains } } -fn -port [cmd @argv]{ +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=[]) @@ -35,24 +35,24 @@ fn -port [cmd @argv]{ } } -fn -complete-names []{ each [n]{ put $n'@' } $names } +fn -complete-names {|| each {|n| put $n'@' } $names } -fn -complete-domains [name hosts]{ - each [h]{ +fn -complete-domains {|name hosts| + each {|h| put $name@$h } $hosts } -fn -complete-remote-dir [port address dir]{ +fn -complete-remote-dir {|port address dir| try { - ssh -p $port $address 'ls '$dir | each [f]{ + ssh -p $port $address 'ls '$dir | each {|f| put $address':'$dir$f } } except e { } } -fn -complete-args [hosts cmd @argv]{ +fn -complete-args {|hosts cmd @argv| var arg = $argv[-1] var i = (str:index $arg @) if (< $i 0) { @@ -87,7 +87,7 @@ fn -complete-args [hosts cmd @argv]{ -complete-domains $n $hosts } -fn complete [@argv]{ +fn complete {|@argv| var @hosts = (-kh) var cmd = $argv[0] var is-ssh = (eq $cmd ssh) diff --git a/lib/moi/completion/sudo.elv b/lib/moi/completion/sudo.elv index 235550f..3d95d63 100644 --- a/lib/moi/completion/sudo.elv +++ b/lib/moi/completion/sudo.elv @@ -1,4 +1,4 @@ -fn complete [@argv]{ +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 { diff --git a/lib/moi/completion/use.elv b/lib/moi/completion/use.elv index 480da25..5662cfb 100644 --- a/lib/moi/completion/use.elv +++ b/lib/moi/completion/use.elv @@ -12,13 +12,13 @@ var builtin_modules = [ unix ] -set edit:completion:arg-completer[use] = [@argv]{ +set edit:completion:arg-completer[use] = {|@argv| use str use path all $builtin_modules - put $libdir/**.elv | each [f]{ + put $libdir/**.elv | each {|f| if (path:is-regular $f) { str:trim-prefix $f $libdir/ } - } | each [f]{ str:trim-suffix $f .elv } + } | each {|f| str:trim-suffix $f .elv } } diff --git a/lib/moi/util/common.elv b/lib/moi/util/common.elv index 07b5e63..ff85236 100644 --- a/lib/moi/util/common.elv +++ b/lib/moi/util/common.elv @@ -1,7 +1,7 @@ -edit:add-var ++~ [n]{ + $n 1 } -edit:add-var --~ [n]{ + $n 1 } +edit:add-var ++~ {|n| + $n 1 } +edit:add-var --~ {|n| + $n 1 } -edit:add-var sign~ [n]{ +edit:add-var sign~ {|n| if (> $n 0) { put 1 } elif (< $n 0) { @@ -10,8 +10,8 @@ edit:add-var sign~ [n]{ put 0 } } -edit:add-var neg~ [n]{ * $n -1 } -edit:add-var abs~ [n]{ +edit:add-var neg~ {|n| * $n -1 } +edit:add-var abs~ {|n| if (< $n 0) { neg $n } else { @@ -19,18 +19,18 @@ edit:add-var abs~ [n]{ } } -edit:add-var sum~ [@numbers]{ +edit:add-var sum~ {|@numbers| var s = 0 - each [e]{ set s = (+ $s e) } (to-list $numbers) + each {|e| set s = (+ $s e) } (to-list $numbers) } -fn -minmax [t @numbers]{ +fn -minmax {|t @numbers| var l = (to-list $@numbers) if (== (count $l) 0) { return } var m = $l[0] - each [n]{ + each {|n| if ($t $n $m) { set m = $n } @@ -38,5 +38,5 @@ fn -minmax [t @numbers]{ put $m } -edit:add-var min~ [@numbers]{ -minmax $'<~' $@numbers } -edit:add-var max~ [@numbers]{ -minmax $'>~' $@numbers } +edit:add-var min~ {|@numbers| -minmax $'<~' $@numbers } +edit:add-var max~ {|@numbers| -minmax $'>~' $@numbers } diff --git a/lib/moi/util/condition.elv b/lib/moi/util/condition.elv index 4085259..93f5e5f 100644 --- a/lib/moi/util/condition.elv +++ b/lib/moi/util/condition.elv @@ -1,4 +1,4 @@ -fn cset [c t f]{ +fn cset {|c t f| if $c { put $t } else { @@ -6,11 +6,11 @@ fn cset [c t f]{ } } -fn mset [c t f]{ +fn mset {|c t f| all (cset $c $t $f) } -fn call [c t f @argv]{ +fn call {|c t f @argv| var v = (cset $c $t $f) if (is (kind-of $v) fn) { $v $@argv diff --git a/lib/moi/util/file.elv b/lib/moi/util/file.elv index f0552fe..4497d3b 100644 --- a/lib/moi/util/file.elv +++ b/lib/moi/util/file.elv @@ -3,16 +3,16 @@ 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 } + &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]{ +fn -less {|f1 f2| var fl1 fl2 = (str:to-lower $f1) (str:to-lower $f2) var c = (str:compare $fl1 $fl2) if (== $c 0) { @@ -21,7 +21,7 @@ fn -less [f1 f2]{ put (< $c 0) } -fn -get-results [sort result]{ +fn -get-results {|sort result| if $sort { keys $result | order $-less~ } else { @@ -29,14 +29,14 @@ fn -get-results [sort result]{ } } -fn -search [&sort=$false &type=exact @motive]{ +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]{ + peach {|m| try { - $f $m | peach [e]{ + $f $m | peach {|e| set result[$e] = $nil } } except e { } @@ -45,12 +45,12 @@ fn -search [&sort=$false &type=exact @motive]{ -get-results $sort $result } -fn match-files [&sort=$false &type=prefix @motive]{ -search &sort=$sort &type=$type $@motive } +fn match-files {|&sort=$false &type=prefix @motive| -search &sort=$sort &type=$type $@motive } -fn match-extensions [&sort=$false &type=deep-prefix motive @extensions]{ +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) { + -search &type=$type $motive | peach {|f| + if (list:contains {|e| str:has-suffix $f .$e } $extensions) { set result[$f] = $nil } } diff --git a/lib/moi/util/ip.elv b/lib/moi/util/ip.elv index dfe701f..a1157a1 100644 --- a/lib/moi/util/ip.elv +++ b/lib/moi/util/ip.elv @@ -1,16 +1,16 @@ use re use str -fn is-ipv4 [arg]{ +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 }) + not (str:split '.' $arg | list:contains {|p| > $p 255 }) } } -fn is-ipv6 [arg]{ +fn is-ipv6 {|arg| var p = '[0-9a-fA-F]{1,4}' var g1 g2 = '('$p':)' '(:'$p')' var cases = [ @@ -29,27 +29,27 @@ fn is-ipv6 [arg]{ put (re:match $r $arg) } -fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } +fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) } -fn -long-part6 [p]{ +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]{ +fn -middle-part6 {|p| while (and (> (count $p) 1) (eq $p[0] 0)) { set p = $p[1..] } put $p } -fn -find-max0 [parts]{ +fn -find-max0 {|parts| use ./list var idx s = -1 1 var ci cs f = -1 0 $false - list:loop [i p]{ + list:loop {|i p| if (eq $p 0) { condition:call $f { set cs = (+ $cs 1) } { set ci cs f = $i 1 $true } } elif $f { @@ -65,7 +65,7 @@ fn -find-max0 [parts]{ put $idx $s } -fn long6 [ip]{ +fn long6 {|ip| if (not (is-ipv6 $ip)) { fail 'Not an IPv6' } @@ -82,11 +82,11 @@ fn long6 [ip]{ str:split ':' $ip | each $-long-part6~ | str:join ':' } -fn middle6 [ip]{ +fn middle6 {|ip| str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':' } -fn short6 [ip]{ +fn short6 {|ip| var parts = [(str:split ':' (middle6 $ip))] var i s = (-find-max0 $parts) if (>= $i 0) { @@ -102,13 +102,13 @@ fn short6 [ip]{ str:join ':' $parts } -fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } +fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) } -fn -cmp [e1 e2]{ +fn -cmp {|e1 e2| use ./list use ./condition var c = 0 - list:loop [i p1]{ + list:loop {|i p1| var p2 = $e2[$i] set c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0)) if (!= $c 0) { @@ -118,21 +118,21 @@ fn -cmp [e1 e2]{ put $c } -fn cmp4 [ip1 ip2]{ +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]{ +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]{ +fn cmp {|ip1 ip2| if (is-ipv4 $ip1) { if (is-ipv4 $ip2) { cmp4 $ip1 $ip2 diff --git a/lib/moi/util/list.elv b/lib/moi/util/list.elv index 2e4cfda..0774ef5 100644 --- a/lib/moi/util/list.elv +++ b/lib/moi/util/list.elv @@ -1,27 +1,27 @@ use builtin use str -fn -p [@argv]{ +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 ', ')']' + fail '0 or 1 argument needed given '$c' arguments: ['(each {|e| put '"'$e'"' } $argv | str:join ', ')']' } } -fn -r [step begin end]{ +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 } + builtin:range &step=(* $step -1) $end (+ $begin 1) | each {|e| - $d $e } } } -fn to-list [@argv]{ +fn to-list {|@argv| try { -p $@argv } except e { @@ -29,7 +29,7 @@ fn to-list [@argv]{ } } -fn range [&step=1 b @r]{ +fn range {|&step=1 b @r| if (== $step 0) { fail 'bad value: step must be positive, but is 0' } @@ -48,7 +48,7 @@ fn range [&step=1 b @r]{ -r $step $b $e } -fn indexes [&from=$false &step=1 @argv]{ +fn indexes {|&from=$false &step=1 @argv| if (== $step 0) { fail 'bad value: step must be positive, but is 0' } @@ -74,16 +74,16 @@ fn indexes [&from=$false &step=1 @argv]{ } } -fn -for [&from=$false &step=1 @argv]{ +fn -for {|&from=$false &step=1 @argv| var l = (-p $@argv) - indexes &from=$from &step=$step $l | each [i]{ put [ $i $l[$i] ] } + indexes &from=$from &step=$step $l | each {|i| put [ $i $l[$i] ] } } -fn loop [&from=$false &step=1 &end=$false cb @argv]{ +fn loop {|&from=$false &step=1 &end=$false cb @argv| if (not $end) { - -for &from=$from &step=$step $@argv | each [e]{ $cb $@e } + -for &from=$from &step=$step $@argv | each {|e| $cb $@e } } else { - -for &from=$from &step=$step $@argv | each [e]{ + -for &from=$from &step=$step $@argv | each {|e| var i v = $@e if ($end $i $v) { break @@ -92,25 +92,25 @@ fn loop [&from=$false &step=1 &end=$false cb @argv]{ } } } -fn ploop [&from=$false &step=1 cb @argv]{ - -for &from=$from &step=$step $@argv | peach [e]{ $cb $@e } +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 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 empty {|@argv| == (count (-p $@argv)) 0 } +fn not-empty {|@argv| not (empty $@argv) } -fn filter [cb @argv]{ - each [v]{ +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]{ +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 @@ -148,21 +148,21 @@ fn first [&from=$false &reverse=$false cb @argv]{ } } -fn filter-index [cb @argv]{ - loop [i v]{ +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]{ +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]{ + loop &from=$from &step=$step {|i v| if ($cb $v) { set idx = $i break @@ -171,20 +171,20 @@ fn first-index [&from=$false &reverse=$false cb @argv]{ put $idx } -fn search [cb @argv]{ - loop [i v]{ +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]{ +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]{ + loop &from=$from &step=$step {|i v| if ($cb $i $v) { put $v break @@ -192,21 +192,21 @@ fn search-first [&from=$false &reverse=$false cb @argv]{ } $@argv } -fn search-index [cb @argv]{ - loop [i v]{ +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]{ +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]{ + loop &from=$from &step=$step {|i v| if ($cb $i $v) { set idx = $i break @@ -215,9 +215,9 @@ fn search-first-index [&from=$false &reverse=$false cb @argv]{ put $idx } -fn contains [cb @argv]{ +fn contains {|cb @argv| var e = $false - each [v]{ + each {|v| if ($cb $v) { set e = $true break @@ -225,11 +225,11 @@ fn contains [cb @argv]{ } (-p $@argv) put $e } -fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv } +fn contains-not {|cb @argv| contains {|v| not ($cb $v) } $@argv } -fn exists [cb @argv]{ +fn exists {|cb @argv| var e = $false - loop [i v]{ + loop {|i v| if ($cb $i $v) { set e = $true break @@ -237,38 +237,38 @@ fn exists [cb @argv]{ } $@argv put $e } -fn exists-not [cb @argv]{ exists [i v]{ $cb $i $v } $@argv } +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 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) +fn reduce {|v cb @argv| + each {|e| set v = ($cb $v $e) } (to-list $@argv) put $v } -fn remove-duplicate [@argv]{ +fn remove-duplicate {|@argv| var done = [&] - each [v]{ + each {|v| put [&v=$v &e=(has-key $done $v)] set done[$v] = $nil - } (-p $@argv) | each [v]{ + } (-p $@argv) | each {|v| assoc $v k (not $v[e]) - } | each [v]{ + } | each {|v| if $v[k] { put $v[v] } } } -fn premove-duplicate [@argv]{ +fn premove-duplicate {|@argv| var done = [&] - peach [v]{ + peach {|v| set done[$v] = $nil } (-p $@argv) keys $done } -fn swap [i j @argv]{ +fn swap {|i j @argv| var l = (-p $@argv) var c = (count $l) set i j = (-i $i $c) (-i $j $c) @@ -289,8 +289,8 @@ fn swap [i j @argv]{ } } -fn less [cb]{ - var l = [a b]{ < ($cb $a $b) 0 } +fn less {|cb| + var l = {|a b| < ($cb $a $b) 0 } put $l } -fn sort [cb @argv]{ order &less-than=(less $cb) (-p $@argv) } +fn sort {|cb @argv| order &less-than=(less $cb) (-p $@argv) } diff --git a/lib/moi/util/map.elv b/lib/moi/util/map.elv index aaafc45..4d3497f 100644 --- a/lib/moi/util/map.elv +++ b/lib/moi/util/map.elv @@ -1,9 +1,9 @@ -fn empty [container]{ == (count $container) 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 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]{ +fn value-of {|container k &default=$nil| if (has-key $container $k) { put $container[$k] } else { @@ -11,54 +11,54 @@ fn value-of [container k &default=$nil]{ } } -fn unzip [container]{ +fn unzip {|container| var lkeys lvalues = [] [] - keys $container | each [k]{ + keys $container | each {|k| set lkeys = [(all $lkeys) $k] set lvalues = [(all $lvalues) $container[$k]] } put $lkeys $lvalues } -fn zip [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]{ + range $c | peach {|i| put [&k=$lkeys[$i] &i=$i] - } | peach [e]{ + } | peach {|e| assoc $e v $lvalues[$e[i]] - } | each [e]{ + } | each {|e| set result[$e[k]] = $e[v] } put $result } -fn to-map [@argv]{ +fn to-map {|@argv| use ./list var container = (list:to-list $@argv) zip [(range (count $container))] $container } -fn to-set [@argv]{ +fn to-set {|@argv| use ./list var m = [&] - each [k]{ set m[$k] = $nil } (list:to-list $@argv) + each {|k| set m[$k] = $nil } (list:to-list $@argv) put $m } -fn mdissoc [container @argv]{ - each [k]{ +fn mdissoc {|container @argv| + each {|k| set container = (dissoc $container $k) } $argv put $container } -fn massoc [&replace=$true container @argv]{ - each [e]{ +fn massoc {|&replace=$true container @argv| + each {|e| var k v = $@e if (or $replace (not (has-key $container $k))) { set container[$k] = $v @@ -67,7 +67,7 @@ fn massoc [&replace=$true container @argv]{ put $container } -fn add [container key @values]{ +fn add {|container key @values| use ./list var values = (list:to-list $values) if (has-key $container $key) { @@ -78,17 +78,17 @@ fn add [container key @values]{ put $container } -fn join [container container2]{ - keys $container2 | each [k]{ +fn join {|container container2| + keys $container2 | each {|k| set container[$k] = $container2[$k] } put $container } -fn sorted-keys [&reverse=$false &less-than=$nil 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]{ +fn sorted-values {|&reverse=$false &less-than=$nil container| values $container | order &reverse=$reverse &less-than=$less-than } diff --git a/lib/moi/util/option.elv b/lib/moi/util/option.elv index 4947468..1134f10 100644 --- a/lib/moi/util/option.elv +++ b/lib/moi/util/option.elv @@ -2,14 +2,14 @@ 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 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 -splitm {|o| each {|e| put -$e } $o[1:] } -fn -splitl [o]{ +fn -splitl {|o| if (str:contains $o '=') { var opt @value = (str:split '=' $o) put $opt @@ -19,7 +19,7 @@ fn -splitl [o]{ } } -fn split [o]{ +fn split {|o| if (is-moptions $o) { -splitm $o } elif (is-loption $o) { @@ -29,17 +29,17 @@ fn split [o]{ } } -fn -joins [opt]{ - var o = (each [o]{ put $o[1:] } $opt | str:join '') +fn -joins {|opt| + var o = (each {|o| put $o[1:] } $opt | str:join '') if (not-eq $o '') { put -$o } } -fn join [@opt]{ +fn join {|@opt| var opt = (list:-p $@opt) - var cb = [o]{ or (is-soption $o) (is-moptions $o) } + var cb = {|o| or (is-soption $o) (is-moptions $o) } var idx = [(list:filter-index-not $cb $opt)] var i0 = 0 - each [i]{ + each {|i| -joins $opt[{$i0}:$i] put $opt[$i] set i0 = (+ $i 1) @@ -47,9 +47,9 @@ fn join [@opt]{ -joins $opt[{$i0}:] } -fn expand [@argv]{ each $split~ (list:-p $@argv) } +fn expand {|@argv| each $split~ (list:-p $@argv) } -fn map [@argv]{ +fn map {|@argv| use ./map set argv = [(expand $@argv)] var result i c = [&] 0 (count $argv) diff --git a/lib/moi/util/strutil.elv b/lib/moi/util/strutil.elv index 9a9fdc8..304e4a2 100644 --- a/lib/moi/util/strutil.elv +++ b/lib/moi/util/strutil.elv @@ -1,11 +1,11 @@ use str use ./list -fn -repeat [n s]{ +fn -repeat {|n s| repeat $n $s | str:join '' } -fn left [size s]{ +fn left {|size s| var ss = (count $s) var d = (- $size $ss) if (<= $d 0) { @@ -15,7 +15,7 @@ fn left [size s]{ } } -fn right [size s]{ +fn right {|size s| var ss = (count $s) var d = (- $size $ss) if (<= $d 0) { @@ -25,7 +25,7 @@ fn right [size s]{ } } -fn center [size s]{ +fn center {|size s| use math var ss = (count $s) var d = (- $size $ss) @@ -40,12 +40,12 @@ fn center [size s]{ } } -fn -c2map [col]{ +fn -c2map {|col| if (not-eq (kind-of $col) list) { put [] return } - list:loop [i c]{ + list:loop {|i c| var k = (kind-of $c) var cf = [&min=0] if (is $k string) { @@ -63,19 +63,19 @@ fn -c2map [col]{ put $col } -fn -parsecols [col &split=$false lst]{ +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) ] + set lines = [ (eawk {|_ @args| put $args } $lst) ] } - each [l]{ + each {|l| var cl = (count $l) if (> $cl $cc) { - set col = [ $@col (range (- $cl $cc) | each [_]{ put [&align=left &min=0] }) ] + set col = [ $@col (range (- $cl $cc) | each {|_| put [&align=left &min=0] }) ] set cc = $cl } - list:loop [i c]{ + list:loop {|i c| var ccl = (+ (count $c) 1) if (> $ccl $col[$i][min]) { set col[$i][min] = $ccl @@ -85,7 +85,7 @@ fn -parsecols [col &split=$false lst]{ put $col } -fn -format [f e]{ +fn -format {|f e| var cb = $left~ if (has-key $f align) { var a = $f[align] @@ -98,17 +98,17 @@ fn -format [f e]{ $cb $f[min] $e } -fn -formatline [col line]{ - list:loop [i v]{ +fn -formatline {|col line| + list:loop {|i v| -format $col[$i] $v } $line | str:join '' } -fn format [&col=[] &split=$false @lst]{ +fn format {|&col=[] &split=$false @lst| var lines = (list:-p $@lst) if $split { - set lines = [ (eawk [l @args]{ put $args } $lines) ] + set lines = [ (eawk {|l @args| put $args } $lines) ] } set col = (-parsecols $col $lines) - each [l]{ -formatline $col $l } $lines + each {|l| -formatline $col $l } $lines }