From: Ralph Ronnquist Date: Mon, 30 May 2022 00:06:43 +0000 (+1000) Subject: use EDITOR when defined X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=21c10f50ec46f20fc518ff444d39ead205d04eb6;p=rrq%2Fnewlisp-ftw.git use EDITOR when defined --- diff --git a/enitool.lsp b/enitool.lsp index 0efd797..2511142 100755 --- a/enitool.lsp +++ b/enitool.lsp @@ -20,6 +20,9 @@ ;; regex to identify block starters 'ENI-HEAD (format "^\\s*#?\\s*(%s)" (join ENI-KEY "|")) 'ENI-COMMENT "^\\s*#" + 'PROC (if (exec (format "command -v %s" (or (env "EDITOR") "nano"))) ($it 0) + "/bin/nano") + 'SUDO (if (exec "command -v sudo") ($it 0) "") ) (define (is-eni-key PAT S) @@ -72,10 +75,8 @@ (setf PATH '(( 0 "/etc/network/interfaces" )) ) ; Edit a file -(define (edit-file I FILE SUDO) - (let ((PROC (if SUDO "/usr/bin/sudo /bin/nano" "/bin/nano"))) - (wait-pid (process (format "%s -F -J 75 -I -l +%d %s" PROC (int I) FILE))) - )) +(define (edit-file I FILE) + (wait-pid (process (format "%s %s +%d %s" SUDO PROC (int I) FILE)))) (define (ensure-newline TXT) (if (empty? TXT) "" (ends-with TXT "\n") TXT (string TXT "\n"))) @@ -86,12 +87,12 @@ (write-file TXT (string (join (0 B DATA) "\n" true) (ensure-newline (read-file TXT)) (join (E DATA) "\n"))) - (exec (format "sudo mv %s %s" TXT FILE)) + (exec (format "%s mv %s %s" SUDO TXT FILE)) )) (define (key-command-select I FILE) ; PATH (letn ((BLOCK (find-block (- (int I) 1) FILE)) - (TMP "/tmp/enitool.conf") + (TMP "/tmp/enitool/tmp.conf") (HEAD (BLOCK (- (BLOCK 1) (BLOCK 0) -3))) (TAG (or (and (regex "^#?(\\w*) (.*)" HEAD 0) $1) "#")) (VALUE $2)) @@ -107,14 +108,14 @@ (define (delete-block-maybe I FILE) (let ((BLOCK (find-block (- (int I) 1) FILE)) - (TMP "/tmp/enitool.conf")) + (TMP "/tmp/enitool/tmp.conf")) (when (= (3 BLOCK) '("")) - (exec (format "sudo ed %s" FILE) + (exec (format "%s ed %s" SUDO FILE) (format "%dd\nw\n" (+ 1 (BLOCK 0))))))) (define (toggle-commenting I FILE) (let ((BLOCK (find-block (- (int I) 1) FILE)) - (TMP "/tmp/enitool.conf")) + (TMP "/tmp/enitool/tmp.conf")) (letn ((H (- (BLOCK 1) (BLOCK 0))) (TXT (3 BLOCK)) (toggle (if (starts-with (TXT H) "#") @@ -133,13 +134,16 @@ ((member $2 '("KEY_RIGHT" "RETURN")) (key-command-select $1 FILE)) ((= $2 "d") (delete-block-maybe $1 FILE)) ((= $2 "#") (toggle-commenting $1 FILE)) - ((= $2 "e") (edit-file $1 FILE "sudo")) + ((= $2 "e") (edit-file $1 FILE SUDO)) ))) (define (iselect POS FILE) (exec (format "iselect -n '%s' -t '%s' -a -P -K '-k#' -kd -ke -p %d < %s" "enitool" FILE (int POS) FILE))) +(change-dir "/etc/network") +(wait-pid (process (format "%s mkdir -m 777 -p /tmp/enitool" SUDO))) + (while PATH (let ((SEL (apply iselect (PATH 0))) (FILE (PATH 0 1))) (if SEL (command-dispatch (SEL 0) FILE)