How do I turn filenames with line numbers into hyperlinks?

I am a newbie user of emacs and I am currently trying to set up a working environment for python. I am using a rope, but I came across this: although the Find Occurrences command works great, its result is placed in a modeless buffer and I have to copy the filenames to access them.

Buffer content here

As far as I can tell, closes the functionality with what I want (i.e. opening the file at a given line after clicking on it or pressing RET) is provided in compile mode. However, since this is the case, turning on compile mode only causes file names to be allocated.

If I figured it out correctly, in order to process strings, I need to provide items in compilation-error-regexp-alist, as is done in the following snippet (from emacs wiki

(require 'compile)

(let ((symbol  'compilation-ledger)
      (pattern '("^Error: \"\\([^\"\n]+?\\)\", line \\([0-9]+\\):" 1 2)))
  (cond ((eval-when-compile (boundp 'compilation-error-regexp-systems-list))
         ;; xemacs21
         (add-to-list 'compilation-error-regexp-alist-alist
                      (list symbol pattern))
         (compilation-build-compilation-error-regexp-alist))
        ((eval-when-compile (boundp 'compilation-error-regexp-alist-alist))
         ;; emacs22 up
         (add-to-list 'compilation-error-regexp-alist symbol)
         (add-to-list 'compilation-error-regexp-alist-alist
                      (cons symbol pattern)))
        (t
         ;; emacs21
         (add-to-list 'compilation-error-regexp-alist pattern))))

      

How do I change it to work with my buffer?

Are there any better / faster alternatives?

+3


source to share


1 answer


Generally, the fastest way to open a file is when its name appears in the buffer,

M-x ffap

      

(short for M-x find-file-at-point

)

If you want to open the file automatically, you can define your own function:

(defun open-file-at-point ()
  (interactive)
  (let ((file (ffap-file-at-point)))
    (if file
        (find-file file)
      (error "No file at point"))))

      



and maybe bind it to a key with

(global-set-key (kbd "C-<return>") 'open-file-at-point)

      

If you want to use compilation-mode

, you need to add the appropriate regex to compilation-error-regexp-alist(-alist)

. For your example, the following seems to work:

(add-to-list
 'compilation-error-regexp-alist
 'python-file-name)

(add-to-list
 'compilation-error-regexp-alist-alist
 (list
  'python-file-name
  (concat "\\(?1:.*?\\)"              ;; file name
          " : "                       ;; seperator
          "\\(?2:[[:digit:]]+\\)")    ;; line number
   1 2)) ;; subexpr 1 is the file name, subexp 2 is the line number

      

+4


source







All Articles