[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PROPOSAL: New function ffap-ip-at-point

From: Iñigo Serna
Subject: PROPOSAL: New function ffap-ip-at-point
Date: Fri, 22 Jul 2022 10:49:30 +0200
User-agent: mu4e 1.8.6; emacs 29.0.50


I propose the addition of this `ffap-ip-at-point' function.
Equivalent to (and based on) `ffap-machine-at-point', it will return
the IP address at point if it exists, or nil.

The attached patch also includes a very minor change to
`dns-lookup-host' command in order to use it if ffap-machine-at-point
first fails.

Patch is against master from a couple of hours ago.
This is my first serious contribution to emacs, so I'm not sure it's everything ok. What do you think?
Btw, I signed FSF papers in 2021, so no problem here I guess.

Iñigo Serna
diff --git a/lisp/ffap.el b/lisp/ffap.el
index 9de0dd40d1..a0751fbd8e 100644
--- a/lisp/ffap.el
+++ b/lisp/ffap.el
@@ -641,6 +641,25 @@ ffap-fixup-url
    ((and ffap-url-unwrap-remote (ffap-url-unwrap-remote url)))

+;;; IP Address (`ffap-ip-p'):
+(defun ffap-ip-at-point ()
+  "Return IP address at point if it exists, or nil."
+  (let ((mach (ffap-string-at-point 'ip)))
+    (and (ffap-ip-p mach) mach)))
+(defun ffap-ip-p (ip)
+  "Decide whether IP is a valid IP 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) "\\."))))
+    (message "|%S|%S|%S|" start end (length ip))
+    (and (zerop start)
+         (length= ip end)
+         (seq-every-p #'(lambda (num) (and (>= num 0) (<= num 255))) nums))))
 ;;; File Name Handling:
@@ -1094,6 +1113,8 @@ ffap-string-at-point-mode-alist
     (nocolon "--9$+<>@-Z_[:alpha:]~" "<@" "@>;.,!?")
     ;; A machine:
     (machine "-[:alnum:]." "" ".")
+    ;; An IP address:
+    (ip "[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..4bcfcb122d 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."
-   (list (let ((default (ffap-machine-at-point)))
+   (list (let ((default (or (ffap-machine-at-point) (ffap-ip-at-point))))
            (read-string (format-prompt "Lookup host" default) nil nil default))
          (if current-prefix-arg (read-from-minibuffer "Name server: "))))
   (let ((options

reply via email to

[Prev in Thread] Current Thread [Next in Thread]