new stuff
[emacs-init.git] / setup / ffap.el
1 (defun my-find-file-at-point-with-line ()
2   "Opens the file at point and goes to line-number."
3   (interactive)
4   (let ((fname (ffap-file-at-point)))
5     (if fname
6       (let ((line
7              (save-excursion
8                (goto-char (cadr ffap-string-at-point-region))
9                (and (re-search-backward ":\\([0-9]+\\)"
10                                         (line-beginning-position) t)
11                     (string-to-int (match-string 1))))))
12         ;; (message "file:%s,line:%s" fname line)
13         (when (and (tramp-tramp-file-p default-directory)
14                    (= ?/ (aref fname 0)))
15           ;; if fname is an absolute path in remote machine, it will not return a tramp path,fix it here.
16           (let ((pos (position ?: default-directory)))
17             (if (not pos) (error "failed find first tramp indentifier ':'"))
18             (setf pos (position ?: default-directory :start (1+ pos)))
19             (if (not pos) (error "failed find second tramp indentifier ':'"))
20             (setf fname (concat (substring default-directory 0 (1+ pos)) fname))))
21         (message "fname:%s" fname)
22         (find-file-existing fname)
23         (when line (goto-line line)))
24       (error "File does not exist."))))
25
26 (global-set-key (kbd "C-c C-x C-f") 'my-find-file-at-point-with-line)