Subject: bug#24057: 25.1.50; ffap interprets comments beginning with "//" as file path

From: Kaushal Modi <[email-masked]>
Date: Fri, 22 Jul 2016 23:00:00 +0000

Hi all,

I lived with this bug for a long time and then I lived with a patched
ffap-string-at-point for a long time :)

The bug is that if

(1) a major mode uses "//" as comment start chars, and
(2) ido-mode is enabled, and
(3) ffap feature is enabled, and
(4) user does C-x C-f on a comment like "//foo"

attempt is made to access "//foo" path.

It's a very corner case bug, but I hit is very often as I work most of the
time in a major mode that uses "//" to comment lines (verilog-mode).

Here is the recipe to reproduce this in emacs -Q:

(1) emacs -Q
(2) Evaluate:

(progn
  (c-mode)
  (insert "//This is a comment")
  (ido-mode)
  (setq ido-use-filename-at-point 'guess)
  (re-search-backward "//" (line-beginning-position)))

(3) Hit "C-x C-f"

You will see that emacs is trying to look for a path "//This"!

This must not happen.

The proposed fix adds a little intelligence to skip the comment start chars
in the string stored to ffap-string-at-point var.

Here is the proposed patch generated ignoring white space. I will post a
proper git formatted patch in a follow up email.

=====

diff --git a/lisp/ffap.el b/lisp/ffap.el
index 7013e6e..ba62012 100644
--- a/lisp/ffap.el
+++ b/lisp/ffap.el
@@ -1097,29 +1097,50 @@ ffap-string-at-point

 (defun ffap-string-at-point (&optional mode)
   "Return a string of characters from around point.
+
 MODE (defaults to value of `major-mode') is a symbol used to look up
 string syntax parameters in `ffap-string-at-point-mode-alist'.
+
 If MODE is not found, we use `file' instead of MODE.
+
 If the region is active, return a string from the region.
-Sets the variable `ffap-string-at-point' and the variable
-`ffap-string-at-point-region'."
+
+If the point is in a comment, ensure that the returned string does not
contain
+the comment start characters (especially for major modes that have '//' as
+comment start characters).
+
+Sets variables `ffap-string-at-point' and `ffap-string-at-point-region'. "
   (let* ((args
           (cdr
            (or (assq (or mode major-mode) ffap-string-at-point-mode-alist)
                (assq 'file ffap-string-at-point-mode-alist))))
+         (region-selected (use-region-p))
          (pt (point))
- (beg (if (use-region-p)
+         (beg (if region-selected
                   (region-beginning)
                 (save-excursion
                   (skip-chars-backward (car args))
                   (skip-chars-forward (nth 1 args) pt)
                   (point))))
- (end (if (use-region-p)
+         ;; If point is in a comment like "//abc" (in `c-mode'), and a
+         ;; region is not selected, return the position of 'a'.
+         (comment-start-pos (unless region-selected
+                              (save-excursion
+                                (goto-char beg)
+                                (comment-search-forward
+                                 (line-end-position) :noerror)
+                                (point))))
+         (end (if region-selected
                   (region-end)
                 (save-excursion
                   (skip-chars-forward (car args))
                   (skip-chars-backward (nth 2 args) pt)
                   (point)))))
+    (when (and comment-start-pos
+               (> end comment-start-pos))
+      (setq beg comment-start-pos))
+    ;; (message "comment-start-pos = %d end = %d beg = %d"
+    ;;          comment-start-pos end beg)
     (setq ffap-string-at-point
           (buffer-substring-no-properties
            (setcar ffap-string-at-point-region beg)

=====

In GNU Emacs 25.1.50.10 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23)
 of 2016-07-22 built on ...
Repository revision: 03f32876210f3dd68c71baa210e523c3b7581758
Windowing system distributor 'The X.Org Foundation', version 11.0.60900000
System Description: Red Hat Enterprise Linux Workstation release 6.6
(Santiago)

Configured using:
 'configure --with-modules
 --prefix=/home/kmodi/usr_local/apps/6/emacs/master
 'CPPFLAGS=-fgnu89-inline -I/home/kmodi/usr_local/6/include
 -I/usr/include/freetype2 -I/usr/include' 'CFLAGS=-ggdb3 -O0'
 'CXXFLAGS=-ggdb3 -O0' 'LDFLAGS=-L/home/kmodi/usr_local/6/lib
 -L/home/kmodi/usr_local/6/lib64 -ggdb3''

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GCONF GSETTINGS
NOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE LIBOTF XFT ZLIB
TOOLKIT_SCROLL_BARS GTK2 X11 MODULES

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=none
  locale-coding-system: utf-8-unix

-- 

Kaushal Modi