diff options
author | Kaushal Modi <kaushal.modi@gmail.com> | 2017-03-17 18:03:23 -0400 |
---|---|---|
committer | Noam Postavsky <npostavs@gmail.com> | 2017-03-23 08:57:13 -0400 |
commit | e472cfe8f3b01f29a49614f6207e4128e8b36b8c (patch) | |
tree | b182bf3006a1e1e23f977f3fe64ce59cb7288474 /lisp | |
parent | fe3af8d4f2a4cd67958f76d1b708e8a78e68cd4f (diff) | |
download | emacs-e472cfe8f3b01f29a49614f6207e4128e8b36b8c.tar.gz emacs-e472cfe8f3b01f29a49614f6207e4128e8b36b8c.tar.bz2 emacs-e472cfe8f3b01f29a49614f6207e4128e8b36b8c.zip |
Do not include comment start chars in ffap string
* lisp/ffap.el (ffap-string-at-point): 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). Otherwise, in a major mode like c-mode, with `ido-mode'
enabled and `ido-use-filename-at-point' set to `guess', doing "C-x
C-f" on a "//foo" comment will initiate an attempt to access a path
"//foo" (Bug#24057).
Co-authored-by: Noam Postavsky <npostavs@gmail.com>
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/ffap.el | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/lisp/ffap.el b/lisp/ffap.el index d7222bfb681..1ea32b75f12 100644 --- a/lisp/ffap.el +++ b/lisp/ffap.el @@ -1110,32 +1110,67 @@ The arguments CHARS, BEG and END are handled as described in (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. -Set the variable `ffap-string-at-point' and the variable + +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). + +Set the variables `ffap-string-at-point' and `ffap-string-at-point-region'. + When the region is active and larger than `ffap-max-region-length', return an empty string, and set `ffap-string-at-point-region' to '(1 1)." (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) + (end (if region-selected (region-end) (save-excursion (skip-chars-forward (car args)) (skip-chars-backward (nth 2 args) pt) (point)))) (region-len (- (max beg end) (min beg end)))) + + ;; If the initial characters of the to-be-returned string are the + ;; current major mode's comment starter characters, *and* are + ;; not part of a comment, remove those from the returned string + ;; (Bug#24057). + ;; Example comments in `c-mode' (which considers lines beginning + ;; with "//" as comments): + ;; //tmp - This is a comment. It does not contain any path reference. + ;; ///tmp - This is a comment. The "/tmp" portion in that is a path. + ;; ////tmp - This is a comment. The "//tmp" portion in that is a path. + (when (and + ;; Proceed if no region is selected by the user. + (null region-selected) + ;; Check if END character is part of a comment. + (save-excursion + (nth 4 (syntax-ppss end)))) + ;; Move BEG to beginning of comment (after the comment start + ;; characters), or END, whichever comes first. + (save-excursion + (let ((state (syntax-ppss beg))) + ;; (nth 4 (syntax-ppss)) will be nil for comment start chars. + (unless (nth 4 state) + (parse-partial-sexp beg end nil nil state :commentstop) + (setq beg (point)))))) + (if (and (natnump ffap-max-region-length) (< region-len ffap-max-region-length)) ; Bug#25243. (setf ffap-string-at-point-region (list beg end) |