Question

I found the function here for replace filen at point but it doesn't seem to work properly: http://www.emacswiki.org/emacs/InsertFileName. It correctly finds the file-at-point but the replace appears to take the file you input and not the file detected originally from what I can see. I'm not sure how to fix this.

If I Run the function on /home/testfile

First it says file to replace so for example: /home/secondfile Then it says replace '/home/secondfile' with: /home/secondfile and then it says: No file at point

Any ideas??

Here is the function:

(autoload 'ffap-guesser "ffap")
(autoload 'ffap-read-file-or-url "ffap")

(defun my-replace-file-at-point (currfile newfile)
"Replace CURRFILE at point with NEWFILE.

  When interactive, CURRFILE will need to be confirmed by user
  and will need to exist on the file system to be recognized,
  unless it is a URL.

  NEWFILE does not need to exist.  However, Emacs's minibuffer
  completion can help if it needs to be.
  " 

(interactive
 (let ((currfile (ffap-read-file-or-url "Replace filename: "
                                        (ffap-guesser))))
   (list currfile
         (ffap-read-file-or-url (format "Replace `%s' with: "
                                        currfile) currfile))))
(save-match-data
  (if (or (looking-at (regexp-quote currfile))
          (let ((filelen (length currfile))
                (opoint (point))
                (limit (+ (point) (length currfile))))
            (save-excursion
              (goto-char (1- filelen))
              (and (search-forward currfile limit
                                   'noerror)
                   (< (match-beginning 0) opoint))
                   (>= (match-end 0) opoint))))
      (replace-match newfile)
    (error "No file at point to replace"))))
Was it helpful?

Solution

There are probably a few things wrong/going on here. The first is, your point position when you are executing this. The second is, if you are using /home/user/something, that there is a strong possibility you will have mismatch between /home/user/something and ~/something (ffap returns the latter while at the point you may have written the former).

First:

The use of looking-at with the regexp quoted filename expects the point to be at the beginning: e.g. |/home/user/something.

Its partner, looking-back expects /home/user/something|. Being somewhere in the middle will throw this error.

One quick fix for this is changing looking-at to thing-at-point-looking-at.

Second:

If you have written /home/user/something, ffap functions (in my case) shorten this using ~. There are probably some settings that govern this, but the easiest, quick fix I know of is using expand-file-name. This will take care of the first case, and if it is written as ~/something, the save-excursion body will replace it in the alternate case.

The only negative result I see is that, you might sometimes replace:

/home/user/something with ~/somethingelse

But, anyways, these two quick fixes just result in this complete change:

(thing-at-point-looking-at (regexp-quote (expand-file-name currfile)))

OTHER TIPS

can't see where "ffap-guesser" is defined. Looks like a bug.

Maybe try instead

"find-file-at-point"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top