--- lisp/files.el +++ lisp/files.el @@ -1816,12 +1816,15 @@ If a buffer exists visiting FILENAME, return that one, but verify that the file has not changed since visited or saved. The buffer is not selected, just returned to the caller. +If a buffer exists visiting FILENAME, and the dynamic variable +buffer-preexisted is bound, set it to t. Optional second arg NOWARN non-nil means suppress any warning messages. Optional third arg RAWFILE non-nil means the file is read literally. Optional fourth arg WILDCARDS non-nil means do wildcard processing and visit all the matching files. When wildcards are actually used and expanded, return a list of buffers that are visiting the various files." + (defvar buffer-preexisted) ; Tell byte compiler about free dynamic var (setq filename (abbreviate-file-name (expand-file-name filename))) @@ -1981,6 +1984,8 @@ (error (if rawfile "File already visited non-literally" "File already visited literally")))))) ;; Return the buffer we are using. + (if (boundp 'buffer-preexisted) + (setq buffer-preexisted t)) buf) ;; Create a new buffer. (setq buf (create-file-buffer filename)) --- lisp/emacs-lisp/find-func.el +++ lisp/emacs-lisp/find-func.el @@ -212,6 +212,7 @@ (indirect-function (find-function-advised-original fun-or-var))))) (with-current-buffer (find-file-noselect file) + (save-excursion (goto-char (point-min)) (unless (re-search-forward (if type @@ -223,7 +224,7 @@ "\"")) nil t) (error "Can't find source for %s" fun-or-var)) - (cons (current-buffer) (match-beginning 0)))) + (cons (current-buffer) (match-beginning 0))))) ;;;###autoload (defun find-library (library) @@ -283,6 +284,7 @@ (let* ((filename (find-library-name library)) (regexp-symbol (cdr (assq type find-function-regexp-alist)))) (with-current-buffer (find-file-noselect filename) + (save-excursion (let ((regexp (format (symbol-value regexp-symbol) ;; Entry for ` (backquote) macro in loaddefs.el, ;; (defalias (quote \`)..., has a \ but @@ -310,7 +312,7 @@ (progn (beginning-of-line) (cons (current-buffer) (point))) - (cons (current-buffer) nil)))))))) + (cons (current-buffer) nil))))))))) ;;;###autoload (defun find-function-noselect (function &optional lisp-only) --- lisp/help-mode.el +++ lisp/help-mode.el @@ -198,11 +198,16 @@ (help-C-file-name (indirect-function fun) 'fun))) ;; Don't use find-function-noselect because it follows ;; aliases (which fails for built-in functions). - (let ((location + (defvar buffer-preexisted) + (let* (buffer-preexisted + (location (find-function-search-for-symbol fun nil file))) (pop-to-buffer (car location)) (if (cdr location) - (goto-char (cdr location)) + (progn (if (and (/= (cdr location) (point)) + buffer-preexisted) + (push-mark)) + (goto-char (cdr location))) (message "Unable to find location in file")))) 'help-echo (purecopy "mouse-2, RET: find function's definition")) @@ -211,8 +216,11 @@ 'help-function (lambda (fun file) (setq file (locate-library file t)) (if (and file (file-readable-p file)) - (progn + (defvar buffer-preexisted) + (let* ((buffer-preexisted + (find-buffer-visiting file))) (pop-to-buffer (find-file-noselect file)) + (if buffer-preexisted (push-mark)) (goto-char (point-min)) (if (re-search-forward (format "^[ \t]*(\\(cl-\\)?define-compiler-macro[ \t]+%s" @@ -227,10 +235,15 @@ 'help-function (lambda (var &optional file) (when (eq file 'C-source) (setq file (help-C-file-name var 'var))) - (let ((location (find-variable-noselect var file))) + (defvar buffer-preexisted) + (let* (buffer-preexisted + (location (find-variable-noselect var file))) (pop-to-buffer (car location)) (if (cdr location) - (goto-char (cdr location)) + (progn (if (and (/= (cdr location) (point)) + buffer-preexisted) + (push-mark)) + (goto-char (cdr location))) (message "Unable to find location in file")))) 'help-echo (purecopy "mouse-2, RET: find variable's definition")) --- lisp/progmodes/etags.el +++ lisp/progmodes/etags.el @@ -1171,9 +1171,11 @@ ;; Get the local value in the tags table buffer before switching buffers. (setq goto-func goto-tag-location-function) + (defvar buffer-preexisted) + (let (buffer-preexisted) (tag-find-file-of-tag-noselect file) (widen) - (push-mark) + (if buffer-preexisted (push-mark))) (funcall goto-func tag-info) ;; Return the buffer where the tag was found. @@ -1183,6 +1185,7 @@ "Find the right line in the specified FILE." ;; If interested in compressed-files, search files with extensions. ;; Otherwise, search only the real file. + (defvar buffer-preexisted) ; Tell byte compiler about free dynamic var (let* ((buffer-search-extensions (if auto-compression-mode tags-compression-info-list '(""))) @@ -1198,6 +1201,8 @@ (while (and (not the-buffer) buffer-search-extensions) (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions)))) (setq buffer-search-extensions (cdr buffer-search-extensions))) + (if (and the-buffer (boundp 'buffer-preexisted)) + (setq buffer-preexisted t)) ;; if found a buffer but file modified, ensure we re-read ! (if (and the-buffer (not (verify-visited-file-modtime the-buffer))) (find-file-noselect (buffer-file-name the-buffer)))