Article View: gnu.emacs.help
Article #189286Re: need lisp function to find filename under point
From: Sebastian Kremer
Date: Mon, 18 Mar 1991 20:02
Date: Mon, 18 Mar 1991 20:02
60 lines
3169 bytes
3169 bytes
>>> On 18 Mar 91 02:09:52 GMT, zoo@APS1.SPA.UMN.EDU said:
zoo> I'd like to be able to do something on the order of this:
zoo> When I'm editing C code, and I want to edit a header file, my cursor
zoo> is often near the #include statement. I'd then like to use some
zoo> function to find the filename of the include statement, and then
zoo> ideally use a search path (local variable with a default to
zoo> ("/usr/local/include" "/usr/include") probably) to visit that file.
zoo> Does anyone have code to do the above? Or at the very least find a
zoo> filename at point?
(define-key ctl-x-map "\C-f" 'find-this-file)
(defun find-this-file (fn)
"Edit file FILENAME.
Switch to a buffer visiting file FILENAME, creating one if none already exists.
Interactively, with a prefix arg, default to filename-at-point.
Useful to edit the file mentioned in the buffer you are editing, or to
test if that file exists: use minibuffer completion after snatching the
name or part of it."
(interactive
(list
(if current-prefix-arg
(let ((fn (filename-at-point)))
(read-file-name "Find file: " fn nil fn))
(read-file-name "Find file: "))))
(find-file (expand-file-name fn)))
(defun filename-at-point ()
"Get the filename closest to point, but don't change your
position. Has a preference for looking backward when not
directly on a symbol."
;; Not at all perfect - point must be right in the name.
(let ((filename-chars ".a-zA-Z0-9---_/:") start end filename
(bol (save-excursion (beginning-of-line) (point)))
(eol (save-excursion (end-of-line) (point))))
(save-excursion
;; first see if you're just past a filename
(if (not (eobp))
(if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
(progn
(skip-chars-backward " \n\t\r({[]})")
(if (not (bobp))
(backward-char 1)))))
(if (string-match (concat "[" filename-chars "]")
(char-to-string (following-char)))
(progn
(skip-chars-backward filename-chars)
(setq start (point))
(if (string-match "[/~]" (char-to-string (preceding-char)))
(setq start (1- start)))
(skip-chars-forward filename-chars))
(error "No file found around point!"))
(expand-file-name (buffer-substring start (point))))))
--
Sebastian Kremer <sk@thp.Uni-Koeln.DE>
Institute of Theoretical Physics, University of Cologne, F. R. of Germany
Technical Details
Message-ID:
<SK.91Mar18200258@sun4.thp.uni-koeln.de>Path:
arc1-ca.pugleaf.net!gmdzi!unido!uni-koeln!aix370.rrz.uni-koeln.de!skReferences:
<9103180209.AA01993@grumpy.spa.umn.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103b8b,e590d84d9c9a65f8 X-Google-Attributes: gid103b8b,public X-Google-ArrivalTime: 1991-03-19 06:47:50 PST Path: arc1-ca.pugleaf.net!gmdzi!unido!uni-koeln!aix370.rrz.uni-koeln.de!sk From: sk@thp.uni-koeln.de (Sebastian Kremer) Newsgroups: gnu.emacs.help Subject: Re: need lisp function to find filename under point Message-ID: <SK.91Mar18200258@sun4.thp.uni-koeln.de> Date: 18 Mar 91 20:02:58 GMT References: <9103180209.AA01993@grumpy.spa.umn.edu> Sender: news@rrz.uni-koeln.de (Usenet News System) Followup-To: gnu.emacs.help Organization: Institute of Theoretical Physics, University of Cologne, F. R. Germany Lines: 56 In-Reply-To: zoo@APS1.SPA.UMN.EDU's message of 18 Mar 91 02:09:52 GMT