From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Expanding list into string within a command Date: Sun, 6 Dec 2020 23:30:43 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35668"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0 (3d08634) (2020-11-07) Cc: Help GNU Emacs To: arthur miller Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 06 21:46:43 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1km0vb-0009BT-04 for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 06 Dec 2020 21:46:43 +0100 Original-Received: from localhost ([::1]:43792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1km0va-0002n8-1K for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 06 Dec 2020 15:46:42 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60698) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1km0uW-0002mB-Fh for help-gnu-emacs@gnu.org; Sun, 06 Dec 2020 15:45:36 -0500 Original-Received: from static.rcdrun.com ([95.85.24.50]:57731) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1km0uU-0001yv-HW for help-gnu-emacs@gnu.org; Sun, 06 Dec 2020 15:45:35 -0500 Original-Received: from localhost ([::ffff:197.157.0.57]) (AUTH: PLAIN admin, TLS: TLS1.2,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by static.rcdrun.com with ESMTPSA id 00000000002C0006.000000005FCD40A1.000025A5; Sun, 06 Dec 2020 20:35:45 +0000 Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=95.85.24.50; envelope-from=bugs@gnu.support; helo=static.rcdrun.com X-Spam_score_int: 14 X-Spam_score: 1.4 X-Spam_bar: + X-Spam_report: (1.4 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:126109 Archived-At: * arthur miller [2020-12-06 22:57]: > "So I was thinking to use macro that will expand the list within a command." > > Try smth like this: > (my-macro (command &rest list) > (dolist (item `,@list) > (dosnth with your list item here))) > > Kind of, if that is what yiu want, I am not sure really what you are after here, but I use that as an idiom quite a lot. Thank you. I had to use `apply' when passing `args' that I get from &rest Now I have made the function better that takes input as string, processes outside command and returns output as string: (defun rcd-command-output-from-input (program input &rest args) "Returns output from PROGRAM INPUT with optional ARGS" (let* ((output (with-temp-buffer (insert input) (apply #'call-process-region nil nil program t t nil args) (buffer-string)))) output)) Which is also handy for some text processing like: (defun markdown (text) (rcd-command-output-from-input "markdown" text)) (markdown "## Hello") "

Hello

" Then I have improved the generic function to accept any `cs2cs' format which is tool for conversion of geographic coordinates: (defun syogm-cs2cs (coord-string &rest cs2cs-format) (let* ((command "cs2cs") (cs2cs (apply #'rcd-command-output-from-input command coord-string cs2cs-format)) (output (string-trim cs2cs)) (output (split-string output "\t")) (lat (pop output)) (output (split-string (car output) " ")) (lon (pop output))) (format "%s %s" lat lon))) And now this works: (syogm-cs2cs "5d15'57.76\"S 35d8'22.65\"E" "-f" "%.6f" "+proj=latlong" "+datum=WGS84" "+to" "+proj=latlong" "+datum=WGS84") Then I can make new functions like: (defun syogm-dms2dd (coord-string) "Convert DMS degree, minutes and seconds notation to DD decimal notation" (syogm-cs2cs coord-string "-f" "%.6f" "+proj=latlong" "+datum=WGS84" "+to" "+proj=latlong" "+datum=WGS84")) Then it converts to acceptable result from degree, minutes, seconds to decimal notation: (syogm-dms2dd "5d15'57.76\"S 35d8'22.65\"E") "-5.266044 35.139625" as then I can convert Ugandan UTM zone 36N with geodetic datum ARC1960 easier to WGS84 system used on mobile devices and maps of today: (defun syogm-convert-arc-1964-utm-36N-to-wgs-84 (earthing-northing) "Converts UTM Zone 36N Arc 1960 geodetic datum to WGS84" (syogm-cs2cs earthing-northing "-f" "%.5f" "EPSG:21096" "EPSG:4326")) (syogm-convert-arc-1964-utm-36N-to-wgs-84 "137878 -125964") "-1.14052 29.74755" Which is final result I wanted to get, as only so I can request download of maps from online providers. Although I have been doing that conversion myself with Common Lisp already, but now I just wish to use external tool. (defun ll-number (d) (let ((l (- (length d) 1))) (if (integerp (read-from-string (first-char d))) (substring d 0 l) d))) (defun ll-parts (coord) (let ((ll-list (split-by-one-space coord))) (delete-if #'string-emptyp ll-list) (setf ll-list (map 'list #'(lambda (s) (string-trim '(#\Space) s)) ll-list)) (setf ll-list (map 'list #'(lambda (s) (ll-number s)) ll-list)) ll-list)) (defun dms2dd-degrees (d) (truncate d)) (defun dms2dd-minutes (m) (float (/ m 60))) (defun dms2dd-seconds (s) (float (/ s 3600))) (defun dms2dd (coord) (let* ((ll-list (ll-parts coord)) (degrees (car ll-list)) (minutes (cadr ll-list)) (seconds (caddr ll-list)) (cardinal (cadddr ll-list)) (dd (+ (dms2dd-degrees (read-from-string degrees)) (dms2dd-minutes (read-from-string minutes)) (dms2dd-seconds (read-from-string seconds))))) (if (or (string= "S" cardinal) (string= "W" cardinal)) (- dd) dd)))