From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?Q?I=C3=B1igo?= Serna Newsgroups: gmane.emacs.devel Subject: Re: PROPOSAL: New function ffap-ip-at-point Date: Fri, 22 Jul 2022 16:46:44 +0200 Message-ID: <87fsit5hh7.fsf@gmx.com> References: <87r12d5xhv.fsf@gmx.com> <83czdxl7gw.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40767"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.8.6; emacs 29.0.50 Cc: Eli Zaretskii , Robert Pluim To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jul 22 16:52:54 2022 Return-path: Envelope-to: ged-emacs-devel@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 1oEu1N-000AOr-JQ for ged-emacs-devel@m.gmane-mx.org; Fri, 22 Jul 2022 16:52:53 +0200 Original-Received: from localhost ([::1]:49930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEu1M-0007gb-9t for ged-emacs-devel@m.gmane-mx.org; Fri, 22 Jul 2022 10:52:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33814) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEu0Q-0006We-35 for emacs-devel@gnu.org; Fri, 22 Jul 2022 10:51:54 -0400 Original-Received: from mout.gmx.net ([212.227.17.22]:33789) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEu0N-0004Or-TR; Fri, 22 Jul 2022 10:51:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1658501509; bh=09GWYPH21j0YK6KlPTkVCJ7Nw8rbJ+VgtolHhBfKTfI=; h=X-UI-Sender-Class:References:From:To:Cc:Subject:Date:In-reply-to; b=PxjITpR0rELnzWM7zjenva0Jln770nZ6rQs/lhAyxvwlrBIXLWJ/hxrur/mDzkRVW sPQQT1vph7zlT1V7tWF+yGx6Pg85WRCY2FS9XV6mQvD5uzuTtOa8ttmYAekh8Tg+Jh YAE4+p+3gP0zQUEfslI5QHedUNFQFAkweyTRC5T8= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from zeus.gmx.com ([79.154.9.68]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MtfJX-1nMetP3Vl9-00v6Cc; Fri, 22 Jul 2022 16:51:48 +0200 In-reply-to: <83czdxl7gw.fsf@gnu.org> X-Provags-ID: V03:K1:XFUZXzZ8U5WzrzVAlW56lWX6oxgZm57DQ3O9Ro+n3ChsNi30dxR kMjcmwQYIsCzSJ0OSKWG312CdXiuZO/NagQpy1CeYl0pGpBE86l9YN3C5tWyegYhEKf4nE5 3qkHpOPMRPVZ7giRP1fn9Sm6YQomXwlnZ3oJocbumZumGV5dCFEWMLZ+8j/E9b1MajuKvQn eYPVxZk7zJWkkMIKYmOog== X-UI-Out-Filterresults: notjunk:1;V03:K0:iuAWHJrbHpw=:CLIigxHfvBhdz8sB+OZoyf ollS6A/G/4NKJkb1j2N3c9AQk9PydGIrNkGI5zRH93gZiQHUI7xIqwyIPPAT//B27vaennRq9 0dbRrwWqtgt+TaKTPwODUyayU/AdJvqvrBMVoiggPTDxgQ742iP8050dXsZrN96ZUCij7+znM n150DFVFwpxU0jYHeq1Ld3mgpkK4u2Dl/kZ2Ou+jmK9nfpiDYSdW5HVfGRz/Z2uA7zRGIsdB3 C1L4WiVjP3YCYbtl5IsZAkBEFCiPVMHpjG7LCITWR0CJIDVuctY9dETxoUq4HZfdUm+/2XBVq d7fdf1TxxYeK64T3J/YQL1TKzpT/OpkRWZwXFe70DT2r4wZRn15RZR01eeCxvuIJR+w2J34F1 fVpNETmmWmhyeBS8ng5c8Uy3bj1tHzeTez7CyFh10zZsu0m94rW/1kSegaTWij9vi94I8rZB2 gI4t06HqiXRs4GFgi178EfJnQAzKNpEhOrodmdTpXyt2UHH4rSKJDyeH8JgdwWOumvaShcnGH 00EVpp8py5QJ0Bf12BsXy2hWipb6HcRC5EFl5XoLIf2caQeT+ENV89Gx0bC+jRkJ7XLfW73kv P8qIWKvCplwjQy3+dsV5DceNom2MluucgOhrqJBpdvn3y8P258IH7WI9zKPoDLFhsT5b9uo28 bDDy7UAuFimpAkHNtn+UX6EiAQbU1XiHB+jERjJIHehO1Vme1xri4EBPIr03cZdh/SuUBzzl5 YIigwayVnXf9pO3ORwW0TpCjOigGyCjFlzSJaWRPw1HFJStZMfpHO+FLmr+mjkHGQow7X0Uh Received-SPF: pass client-ip=212.227.17.22; envelope-from=inigoserna@gmx.com; helo=mout.gmx.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:292445 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Thanks Robert and Eli for your comments and suggestions. On 22 July 2022 at 11:36 +02, Robert Pluim =20 wrote: > IPv6? Parsing IPv6 addresses is much more complex: can be shortened and=20 even one or more leading zeros from any group of hexadecimal digits=20 could be removed. Too much work for this humble patch. > You=CA=BCll need a ChangeLog style commit message.=20 What about this? ################################################## Fri Jul 22 16:37:25 2022 I=C3=B1igo Serna Add functions ffap-ipv4-at-point, ffap-ipv4-p * lisp/ffap.el (ffap-ipv4-at-point): New functions. * lisp/ffap.el (ffap-ipv4-p):=20 * lisp/ffap.el (ffap-string-at-point-mode-alist): Add ipv4=20 mode. * test/lisp/ffap-tests.el (ffap-ipv4-at-point): Add tests. * etc/NEWS: Document new functions. * lisp/net/net-utils.el (dns-lookup-host): try to use ffap-ipv4-at-point if hostname is not found first. ################################################## Anyway, I don't have commit rights so some other one should=20 perform the commit and add the message. On 22 July 2022 at 13:20 +02, Eli Zaretskii wrote: > IPv4 or IPv6 addresses? or both? This should be stated in the=20 > doc > string. It's only for IPv4 addresses. I'll add this information to the doc=20 string. I renamed the functions to 'ffap-ipv4-at-point' and 'ffap-ipv4-p'. > This also needs a NEWS entry. Added. Please check it, as my English is not good. >> +(defun ffap-ip-at-point () >> + "Return IP address at point if it exists, or nil." > > "If it exists" is ambiguous for an IP address, so please try=20 > coming up > with a better wording. Sorry. What about "if it is found at point"? > [...] > Leftover from debugging? Glub! Removed, sorry. > Finally, can you add a couple of tests for this facility? Added. I've attached a new version of the patch. Hope you like it better. Thanks, -- I=C3=B1igo Serna --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=ffap-ip-at-point__dns-host-lookup-v2.diff Content-Transfer-Encoding: quoted-printable diff --git a/etc/NEWS b/etc/NEWS index a143550f03..67a2f9833d 100644 =2D-- a/etc/NEWS +++ b/etc/NEWS @@ -335,6 +335,11 @@ Use something like 'M-x shell RET ssh RET' ins= tead. =0C * Changes in Emacs 29.1 +--- +** New functions 'ffap-ipv4-at-point', 'ffap-ipv4-p' +'ffap-ipv4-at-point' return an IPv4 address if it is found at point, +or nil. 'ffap-ipv4-p' check for a valid IPv4 address. + --- ** Emacs is now capable of editing files with arbitrarily long lines. The display of long lines has been optimized, and Emacs no longer diff --git a/lisp/ffap.el b/lisp/ffap.el index 9de0dd40d1..44e9ed9b1e 100644 =2D-- a/lisp/ffap.el +++ b/lisp/ffap.el @@ -641,6 +641,24 @@ ffap-fixup-url ((and ffap-url-unwrap-remote (ffap-url-unwrap-remote url))) (url))) +=0C +;;; IP Address (`ffap-ipv4-p'): + +;;;###autoload +(defun ffap-ipv4-at-point () + "Return an IPv4 address if it is found at point, or nil." + (let ((mach (ffap-string-at-point 'ipv4))) + (and (ffap-ipv4-p mach) mach))) + +(defun ffap-ipv4-p (ip) + "Decide whether IP is a valid IPv4 address." + (when-let* ((start (string-match "\\([0-9]\\{1,3\\}\\.\\)\\{3\\}[0-9]\\= {1,3\\}" ip)) + (end (match-end 0)) + (nums (mapcar #'string-to-number (split-string (substring i= p start end) "\\.")))) + (and (zerop start) + (length=3D ip end) + (seq-every-p #'(lambda (num) (and (>=3D num 0) (<=3D num 255))) = nums)))) + =0C ;;; File Name Handling: ;; @@ -1094,6 +1112,8 @@ ffap-string-at-point-mode-alist (nocolon "--9$+<>@-Z_[:alpha:]~" "<@" "@>;.,!?") ;; A machine: (machine "-[:alnum:]." "" ".") + ;; An IPv4 address: + (ipv4 "[0-9]." "" ".") ;; Mathematica paths: allow backquotes (math-mode ",-:$+<>@-Z_[:lower:]~`" "<" "@>;.,!?`:") ;; (La)TeX: don't allow braces diff --git a/lisp/net/net-utils.el b/lisp/net/net-utils.el index c7ff175e08..15195a1fdf 100644 =2D-- a/lisp/net/net-utils.el +++ b/lisp/net/net-utils.el @@ -577,7 +577,7 @@ dns-lookup-host This command uses `dns-lookup-program' for looking up the DNS information= ." (interactive - (list (let ((default (ffap-machine-at-point))) + (list (let ((default (or (ffap-machine-at-point) (ffap-ipv4-at-point))= )) (read-string (format-prompt "Lookup host" default) nil nil def= ault)) (if current-prefix-arg (read-from-minibuffer "Name server: ")))) (let ((options diff --git a/test/lisp/ffap-tests.el b/test/lisp/ffap-tests.el index 4b580b5af5..1f9313e274 100644 =2D-- a/test/lisp/ffap-tests.el +++ b/test/lisp/ffap-tests.el @@ -158,6 +158,32 @@ ffap-test-path (goto-char (point-min)) (should (equal (ffap-file-at-point) nil)))) +(ert-deftest ffap-ipv4-at-point () + (with-temp-buffer + (insert "\ +aaaaaaaaaaaaaaaaaaaaaaa +3.14,15:92 +1.X2.3.4 +5.6.7 +8.9.444.2 +1.2.3.4 +bbbb5.6.7.8ccc\n") + (goto-char (point-min)) + (should-not (ffap-ipv4-at-point)) + (forward-line) + (should-not (ffap-ipv4-at-point)) + (forward-line) + (should-not (ffap-ipv4-at-point)) + (forward-line) + (should-not (ffap-ipv4-at-point)) + (forward-line) + (should-not (ffap-ipv4-at-point)) + (forward-line) + (should (equal (ffap-ipv4-at-point) "1.2.3.4")) + (forward-line) + (forward-char 7) + (should (equal (ffap-ipv4-at-point) "5.6.7.8")))) + (provide 'ffap-tests) ;;; ffap-tests.el ends here --=-=-=--