-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvj-compilation-find-file-not-found-hook.el
executable file
·158 lines (145 loc) · 6.9 KB
/
vj-compilation-find-file-not-found-hook.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
;; GPL blah blah
;; A work-around for the feature loss in Emacs 24
;; grep-mode no longer inherits the feature from compilation-mode where it
;; looks for Entering Directory lines. :-(
(require 'compile)
(defvar compilation-find-file-hook nil)
(add-hook 'compilation-find-file-hook 'compilation-find-file-via-entering)
;; Look for "Entering directory `c:/xx/hestehoved'"
(defun compilation-find-file-via-entering (filename)
(let ((result
(save-excursion
(if (re-search-backward
(concat "Ent" "ering directory `\\([^']*\\)'") nil t)
(concat (match-string-no-properties 1) "/"
filename)))))
(if (not result)
(save-excursion
(if (re-search-backward
(concat "^\\(" (replace-regexp-in-string "[/\\]" "." default-directory) ".*[a-z]*\\)")
nil t)
(setq result (match-string-no-properties 1)))))
(if (not result)
(if (string-match "^http:" filename)
(message "URL!")))
(if (and result (file-exists-p result))
(progn
(message "vj: found \"%s\"" result)
(find-file-noselect result)) ;return a buffer
nil)))
(defun compilation-find-file (marker filename directory &rest formats)
"Find a buffer for file FILENAME (***OVERRIDDEN***).
If FILENAME is not found at all, ask the user where to find it.
Pop up the buffer containing MARKER and scroll to MARKER if we ask
the user where to find the file.
Search the directories in `compilation-search-path'.
A nil in `compilation-search-path' means to try the
\"current\" directory, which is passed in DIRECTORY.
If DIRECTORY is relative, it is combined with `default-directory'.
If DIRECTORY is nil, that means use `default-directory'.
FORMATS, if given, is a list of formats to reformat FILENAME when
looking for it: for each element FMT in FORMATS, this function
attempts to find a file whose name is produced by (format FMT FILENAME)."
(or formats (setq formats '("%s")))
(let ((dirs compilation-search-path)
(spec-dir (if directory
(expand-file-name directory)
default-directory))
buffer thisdir fmts name)
(if (and filename
(file-name-absolute-p filename))
;; The file name is absolute. Use its explicit directory as
;; the first in the search path, and strip it from FILENAME.
(setq filename (abbreviate-file-name (expand-file-name filename))
dirs (cons (file-name-directory filename) dirs)
filename (file-name-nondirectory filename)))
;; Now search the path.
(while (and dirs (null buffer))
(setq thisdir (or (car dirs) spec-dir)
fmts formats)
;; For each directory, try each format string.
(while (and fmts (null buffer))
(setq name (file-truename
(file-name-concat thisdir (format (car fmts) filename)))
buffer (and (file-exists-p name)
(find-file-noselect name))
fmts (cdr fmts)))
(setq dirs (cdr dirs)))
;; If we haven't found it, this might be a parallel build.
;; Search the directories further up the buffer.
(when (and (null buffer)
compilation-search-all-directories)
(with-current-buffer (marker-buffer marker)
(save-excursion
(goto-char (marker-position marker))
(when-let ((prev (compilation--previous-directory (point))))
(goto-char prev))
(setq dirs (cdr (or (get-text-property
(1- (point)) 'compilation-directory)
(get-text-property
(point) 'compilation-directory))))))
(while (and dirs (null buffer))
(setq thisdir (car dirs)
fmts formats)
(while (and fmts (null buffer))
(setq name (file-truename
(file-name-concat thisdir (format (car fmts) filename)))
buffer (and (file-exists-p name)
(find-file-noselect name))
fmts (cdr fmts)))
(setq dirs (cdr dirs))))
(when (and (null buffer) compilation-find-file-hook)
(message "Look for %s" filename)
(setq buffer (run-hook-with-args-until-success 'compilation-find-file-hook filename)))
(while (null buffer) ;Repeat until the user selects an existing file.
;; The file doesn't exist. Ask the user where to find it.
(save-excursion ;This save-excursion is probably not right.
(let ((w (let ((pop-up-windows t))
(display-buffer (marker-buffer marker)
'(nil (allow-no-window . t))))))
(with-current-buffer (marker-buffer marker)
(goto-char marker)
(and w (progn (compilation-set-window w marker)
(compilation-set-overlay-arrow w))))
(let* ((name (read-file-name
(format-prompt "Find this %s in"
filename compilation-error)
spec-dir filename t nil
;; The predicate below is fine when called from
;; minibuffer-complete-and-exit, but it's too
;; restrictive otherwise, since it also prevents the
;; user from completing "fo" to "foo/" when she
;; wants to enter "foo/bar".
;;
;; Try to make sure the user can only select
;; a valid answer. This predicate may be ignored,
;; tho, so we still have to double-check afterwards.
;; TODO: We should probably fix read-file-name so
;; that it never ignores this predicate, even when
;; using popup dialog boxes.
;; (lambda (name)
;; (if (file-directory-p name)
;; (setq name (expand-file-name filename name)))
;; (file-exists-p name))
))
(origname name))
(cond
((not (file-exists-p name))
(message "Cannot find file `%s'" name)
(ding) (sit-for 2))
((and (file-directory-p name)
(not (file-exists-p
(setq name (file-truename
(file-name-concat name filename))))))
(message "No `%s' in directory %s" filename origname)
(ding) (sit-for 2))
(t
(setq buffer (find-file-noselect name))))))))
;; Make intangible overlays tangible.
;; This is weird: it's not even clear which is the current buffer,
;; so the code below can't be expected to DTRT here. -- Stef
(dolist (ov (overlays-in (point-min) (point-max)))
(when (overlay-get ov 'intangible)
(overlay-put ov 'intangible nil)))
buffer))
(message "Override compilation-find-file!")