[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Expanding list into string within a command
From: |
arthur miller |
Subject: |
RE: Expanding list into string within a command |
Date: |
Sun, 6 Dec 2020 20:07:59 +0000 |
Maybe I was too fast, didn't show how to use macroexample:
(defmacro with-command (cmdname &rest args)
(dolist (arg args)
.... do your thing here .... ))
Now when you use it :
(with-command ls -l -h -s)
If you really want to parse those args or do something with them. But I think
it is better to build a string and pass them that way to a command.
-------- Originalmeddelande --------
Från: Jean Louis <bugs@gnu.support>
Datum: 2020-12-06 20:47 (GMT+01:00)
Till: Help GNU Emacs <help-gnu-emacs@gnu.org>
Ämne: Expanding list into string within a command
I would like to get this function to work, to convert geographical
coordinates notation from 5d15'57.76"S 35d8'22.65"E to decimal system
by using `proj' software and I wish to make generic function that will
accept any kind of format input to avoid hard coding.
This works well on command line:
$ cs2cs -f "%.6f" +proj=latlong +datum=WGS84 +to +proj=latlong +datum=WGS84
5d15'57.76"S 35d8'22.65"E
-5.266044 35.139625 0.000000
And I know how to make it when hard coded. Here I wish to expand it
maybe with macro.
This is the invocation of the function that I would like to get
working by feeding 2 strings, first being coordinates and second the
`cs2cs' format for conversions of coordinates.
(syogm-cs2cs "5d15'57.76\"S 35d8'22.65\"E" "-f \"%.6f\" +proj=latlong
+datum=WGS84 +to +proj=latlong +datum=WGS84")
Error is:
"Rel. 6.1.0, May 15th, 2019
<cs2cs>:
missing argument for -f
program abnormally terminated
"
Error probably comes from lack of my knowledge how to use macro
expansion as here one could see how I have imagined that it could
expand:
(defun syogm-cs2cs (coord-string cs2cs-format)
(let* ((command "cs2cs")
(output (command-stream-in-out command coord-string `,(string-join
(split-string cs2cs-format) " "))))
output))
So I was thinking to use macro that will expand the list within a
command. Is there different better way to do this?
The below functions are related only in so far to maybe make these
above working.
This function here I could improve. This one is invoked many times by
many programs and I would not like changing it. One way to solve the
issue could be that I test for ARGS if it is list or not and convert
there.
(defun command-stream-in-out (command string &rest args)
(let* ((uid (number-to-string (user-uid)))
(memory-dir (rcd-memory-dir))
(infile (concat memory-dir "command-input")))
(string-to-file-force string infile)
(with-temp-buffer
(apply 'call-process command infile (current-buffer) nil args)
(buffer-string))))
All below functions are only relevant for execution of above
functions.
(defun rcd-memory-dir ()
(let ((xdg-runtime-dir (getenv "XDG_RUNTIME_DIR")))
(if xdg-runtime-dir (slash-add xdg-runtime-dir)
(if (and (file-directory-p "/dev/shm")
(file-writable-p "/dev/shm"))
(slash-add "/dev/shm")))))
(defun string-to-file-force (string file)
"Prints string into file, matters not if file exists. Returns FILE as file
name."
(with-temp-file file
(insert string))
file)
(defun slash-add (path)
"Adds slash `/` quickly on the end of string"
(let ((last (substring (reverse path) 0 1)))
(if (string= last "/") path
(concat path "/"))))