From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#6695: bug#8439: [PATCH] ffap.el -- detect paths with spaces (v4) Date: Fri, 14 Aug 2020 15:08:45 +0200 Message-ID: <87tux574te.fsf@gnus.org> References: <87pqoyaxu0.fsf@blue.sea.net> <87zk3i7tbu.fsf@picasso.cante.net> <20121020075601.GD29154@picasso.cante.net> <20121020084551.GE29154@picasso.cante.net> <87mucmn2gs.fsf@gnus.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35023"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 8439@debbugs.gnu.org, 6695@debbugs.gnu.org To: jari Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 14 15:10:11 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1k6ZTH-0008zx-8Q for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 14 Aug 2020 15:10:11 +0200 Original-Received: from localhost ([::1]:40572 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k6ZTG-0001ww-67 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 14 Aug 2020 09:10:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45434) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k6ZT8-0001wK-PQ for bug-gnu-emacs@gnu.org; Fri, 14 Aug 2020 09:10:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:40624) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k6ZT8-0007Hc-GD for bug-gnu-emacs@gnu.org; Fri, 14 Aug 2020 09:10:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1k6ZT8-0004uf-9y for bug-gnu-emacs@gnu.org; Fri, 14 Aug 2020 09:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 14 Aug 2020 13:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 6695 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 6695-submit@debbugs.gnu.org id=B6695.159741054218783 (code B ref 6695); Fri, 14 Aug 2020 13:10:02 +0000 Original-Received: (at 6695) by debbugs.gnu.org; 14 Aug 2020 13:09:02 +0000 Original-Received: from localhost ([127.0.0.1]:52159 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k6ZSA-0004si-2H for submit@debbugs.gnu.org; Fri, 14 Aug 2020 09:09:02 -0400 Original-Received: from quimby.gnus.org ([95.216.78.240]:41316) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k6ZS5-0004sG-GR; Fri, 14 Aug 2020 09:09:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnus.org; s=20200322; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=D565X9EfVleF++1QbiQNyJaf431tQYuoV7MrNKpWaH0=; b=fsmMRThetE5xtIJvMZycE3vQGZ SmQZSni25T9WHpU7IDk6BXSmvhJLDPU1QNSm+6zxoDjM4alLb3iQr+zBGkFGjpH1NwyxDdo7/sN95 5rmAKGi6alAWFn6rr+Mb/OeSe5IJu9u0OwfaiN4uZCLPlhaD63jK1KgjMBcoEc4++qHM=; Original-Received: from cm-84.212.202.86.getinternet.no ([84.212.202.86] helo=xo) by quimby with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k6ZRv-00028Y-Ol; Fri, 14 Aug 2020 15:08:50 +0200 In-Reply-To: <87mucmn2gs.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sat, 23 Nov 2019 14:31:47 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:185106 Archived-At: Lars Ingebrigtsen writes: > This was seven years ago, and not surprisingly, the patch doesn't apply > any more. I've respun the patch so that it now applies to Emacs 28, and the test cases seem to kinda work? The c: isn't included, but is that to be expected? I removed the Cygwin char translation stuff, because there was some discussion about whether that was needed. So what do people think? Good or bad? Does this work in any way sensibly for people? diff --git a/lisp/ffap.el b/lisp/ffap.el index 4a506207d5..6d40fa8c45 100644 --- a/lisp/ffap.el +++ b/lisp/ffap.el @@ -1109,6 +1109,123 @@ ffap-string-at-point ;; Added at suggestion of RHOGEE (for ff-paths), 7/24/95. "Last string returned by the function `ffap-string-at-point'.") +;; Test cases: (let ((ffap-file-name-with-spaces-flag t)) (ffap-string-at-point)) +;; +;; c:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program here.txt +;; c:/Program Files/Open Text Evaluation Media/Open Text Exceed 14 x86/Program Files/Hummingbird/ +;; c:\Program Files\Open Text Evaluation Media\Open Text Exceed 14 x86\Program Files\Hummingbird\ +;; c:\Program Files\Freescale\CW for MPC55xx and MPC56xx 2.10\PowerPC_EABI_Tools\Command_Line_Tools\CLT_Usage_Notes.txt +;; C:\temp\program.log on Windows or /var/log/program.log on Unix. + +(defvar ffap-file-name-with-spaces-flag (memq system-type '(ms-dos windows-nt)) + "If non-nil, enable looking for paths with spaces in `ffap-string-at-point'. +Enabled in W32 by default.") + +(defun ffap-search-backward-file-end (&optional dir-separator end) + "Search backward position point where file would probably end. +Optional DIR-SEPARATOR defaults to \"/\". The search maximum is +`line-end-position' or optional END point. + +Suppose the cursor is somewhere that might be near end of file, +the guessing would position point before punctuation (like comma) +after the file extension: + + C:\temp\file.log, which contain .... + =============================== (before) + ---------------- (after) + + + C:\temp\file.log on Windows or /tmp/file.log on Unix + =============================== (before) + ---------------- (after) + +The strategy is to search backward until DIR-SEPARATOR which defaults to +\"/\" and then take educated guesses. + +Move point and return point if an adjustment was done." + (unless dir-separator + (setq dir-separator "/")) + (let ((opoint (point)) + point punct end whitespace-p) + (when (re-search-backward + (regexp-quote dir-separator) (line-beginning-position) t) + ;; Move to the beginning of the match.. + (forward-char 1) + ;; ... until typical punctuation. + (when (re-search-forward "\\([][<>()\"'`,.:;]\\)" + (or end + (line-end-position)) + t) + (setq end (match-end 0)) + (setq punct (match-string 1)) + (setq whitespace-p (looking-at "[ \t\r\n]\\|$")) + (goto-char end) + (cond + ((and (string-equal punct ".") + whitespace-p) ;end of sentence + (setq point (1- (point)))) + ((and (string-equal punct ".") + (looking-at "[a-zA-Z0-9.]+")) ;possibly file extension + (setq point (match-end 0))) + (t + (setq point (point))))) + (goto-char opoint) + (when point + (goto-char point) + point)))) + +(defun ffap-search-forward-file-end (&optional dir-separator) + "Search DIR-SEPARATOR and position point at file's maximum ending. +This includes spaces. +Optional DIR-SEPARATOR defaults to \"/\". +Call `ffap-search-backward-file-end' to refine the ending point." + (unless dir-separator + (setq dir-separator "/")) + (let* ((chars ;expected chars in file name + (concat "[^][^<>()\"'`;,#*|" + ;; exclude the opposite as we know the separator + (if (string-equal dir-separator "/") + "\\\\" + "/") + "\t\r\n]")) + (re (concat + chars "*" + (if dir-separator + (regexp-quote dir-separator) + "/") + chars "*"))) + (when (looking-at re) + (goto-char (match-end 0))))) + +(defun ffap-dir-separator-near-point () + "Search backward and forward for closest slash or backlash in line. +Return string slash or backslash. Point is moved to closest position." + (let ((point (point)) + str pos) + (when (looking-at ".*?/") + (setq str "/" + pos (match-end 0))) + (when (and (looking-at ".*?\\\\") + (or (null pos) + (< (match-end 0) pos))) + (setq str "\\" + pos (match-end 0))) + (goto-char point) + (when (and (re-search-backward "/" (line-beginning-position) t) + (or (null pos) + (< (- point (point)) (- pos point)))) + (setq str "/" + pos (1+ (point)))) ;1+ to keep cursor at the end of char + (goto-char point) + (when (and (re-search-backward "\\\\" (line-beginning-position) t) + (or (null pos) + (< (- point (point)) (- pos point)))) + (setq str "\\" + pos (1+ (point)))) + (when pos + (goto-char pos)) + str)) + (defun ffap-string-at-point (&optional mode) "Return a string of characters from around point. @@ -1128,7 +1245,8 @@ ffap-string-at-point 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 + (let* (dir-separator + (args (cdr (or (assq (or mode major-mode) ffap-string-at-point-mode-alist) (assq 'file ffap-string-at-point-mode-alist)))) @@ -1137,14 +1255,25 @@ ffap-string-at-point (beg (if region-selected (region-beginning) (save-excursion - (skip-chars-backward (car args)) - (skip-chars-forward (nth 1 args) pt) + (if (and ffap-file-name-with-spaces-flag + (memq mode '(nil file))) + (when (setq dir-separator (ffap-dir-separator-near-point)) + (while (re-search-backward + (regexp-quote dir-separator) + (line-beginning-position) t) + (goto-char (match-beginning 0)))) + (skip-chars-backward (car args)) + (skip-chars-forward (nth 1 args) pt)) (point)))) (end (if region-selected (region-end) (save-excursion (skip-chars-forward (car args)) (skip-chars-backward (nth 2 args) pt) + (when (and ffap-file-name-with-spaces-flag + (memq mode '(nil file))) + (ffap-search-forward-file-end dir-separator) + (ffap-search-backward-file-end dir-separator)) (point)))) (region-len (- (max beg end) (min beg end)))) -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no