unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Iñigo Serna" <inigoserna@gmx.com>
To: emacs-devel@gnu.org
Cc: Eli Zaretskii <eliz@gnu.org>, Robert Pluim <rpluim@gmail.com>
Subject: Re: PROPOSAL: New function ffap-ip-at-point
Date: Fri, 22 Jul 2022 16:46:44 +0200	[thread overview]
Message-ID: <87fsit5hh7.fsf@gmx.com> (raw)
In-Reply-To: <83czdxl7gw.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1991 bytes --]

Thanks Robert and Eli for your comments and suggestions.


On 22 July 2022 at 11:36 +02, Robert Pluim <rpluim@gmail.com> 
wrote:

> IPv6?

Parsing IPv6 addresses is much more complex: can be shortened and 
even
one or more leading zeros from any group of hexadecimal digits 
could be
removed. Too much work for this humble patch.

> Youʼll need a ChangeLog style commit message. 

What about this?

##################################################
Fri Jul 22 16:37:25 2022  Iñigo Serna  <inigoserna@gmx.com>

Add functions ffap-ipv4-at-point, ffap-ipv4-p

	* lisp/ffap.el (ffap-ipv4-at-point): New functions.
	* lisp/ffap.el (ffap-ipv4-p): 
	* lisp/ffap.el (ffap-string-at-point-mode-alist): Add ipv4 
      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 
perform the
commit and add the message.


On 22 July 2022 at 13:20 +02, Eli Zaretskii <eliz@gnu.org> wrote:

> IPv4 or IPv6 addresses? or both?  This should be stated in the 
> doc
> string.

It's only for IPv4 addresses. I'll add this information to the doc 
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 
> 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ñigo Serna

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ffap-ip-at-point__dns-host-lookup-v2.diff --]
[-- Type: text/x-patch, Size: 3464 bytes --]

diff --git a/etc/NEWS b/etc/NEWS
index a143550f03..67a2f9833d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -335,6 +335,11 @@ Use something like 'M-x shell RET ssh <host> RET' instead.
 \f
 * 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
--- 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)))

+\f
+;;; 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 ip start end) "\\."))))
+    (and (zerop start)
+         (length= ip end)
+         (seq-every-p #'(lambda (num) (and (>= num 0) (<= num 255))) nums))))
+
 \f
 ;;; 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
--- 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 default))
          (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
--- 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

  reply	other threads:[~2022-07-22 14:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-22  8:49 PROPOSAL: New function ffap-ip-at-point Iñigo Serna
2022-07-22  9:36 ` Robert Pluim
2022-07-22 11:20 ` Eli Zaretskii
2022-07-22 14:46   ` Iñigo Serna [this message]
2022-07-22 15:01     ` Eli Zaretskii
2022-07-22 15:04     ` Brian Cully via Emacs development discussions.
2022-07-22 15:28       ` Iñigo Serna
2022-07-22 15:09     ` Lars Ingebrigtsen
2022-07-22 15:34       ` Iñigo Serna
2022-07-22 15:55         ` Robert Pluim
2022-07-22 20:10         ` Lars Ingebrigtsen
2022-07-23  7:31           ` Robert Pluim
2022-07-23  7:37             ` Lars Ingebrigtsen
2022-07-23  7:46               ` Robert Pluim
2022-07-23  7:47                 ` Lars Ingebrigtsen
2022-07-26 12:21                   ` Robert Pluim
2022-07-26 12:48                     ` Eli Zaretskii
2022-07-26 12:54                       ` Robert Pluim
2022-07-23  7:39             ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fsit5hh7.fsf@gmx.com \
    --to=inigoserna@gmx.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rpluim@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).