On Aug 18, 2011, at 1:57 PM, Jonas Frey wrote:
> Thanks a lot for your answer -- it almost works!
>
> I copied the definition that you gave to my .emacs, and now when inverse searching, the cursor jumps to the right line in both files (source file and region file), but it still activates the buffer with the region file.
>
> How can I have the source file active after inverse search?
Oops. That's because I put too much in the (save-excursion ...) Here is a version that doesn't do that. Testing it today I was also having a problem of being off by 3 lines (that's the offset), so I made a change that fixed it for me. If that fix screws things up for you then you can delete the `(forward-line offset)` portion.
You could also add some code to delete the _region_.tex buffer, but I didn't think it was worth it since you're probably going to reopen it again soon anyway.
-Ivan
(defadvice server-goto-line-column (after server-visit-region.tex
(line-col)
activate)
"When visiting _region_.tex from an external
source (e.g. inverse search), go to the original file so that any
changes made won't be lost."
(when (string-match "_region_.tex\\'" (buffer-file-name))
(if (save-excursion ;; in case we don't find the other file we at least want the right line...
(re-search-backward
"\message{ !name(\\([^)]+\\))\\(?: !offset(\\([-0-9]+\\))\\)?\\s *}" nil nil))
(let ((file-name (match-string 1))
(offset (match-string 2))
(line (car (ad-get-arg 0)))
(col (cdr (ad-get-arg 0))))
(if offset (setq line (+ line (string-to-number offset))))
(find-file file-name nil)
;; For some reason this doesn't actually go to the offset
;; line. It's probably because of how advice works.
(server-goto-line-column (cons line col))
(message "Moved from _region_.tex to line %s of %s." line file-name)
(forward-line offset))
;; else
(message "This is _region_.tex, but I couldn't find the original file!"))))