aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuanma Barranquero2002-11-05 07:21:14 +0000
committerJuanma Barranquero2002-11-05 07:21:14 +0000
commit3a64a3cfa0f129550808ea7c5c9f1ba30eba376a (patch)
tree313d16b8f3cd871648c5689b78690252b83b087b
parent44248360429f17451a682548e8c438abc0724253 (diff)
downloademacs-3a64a3cfa0f129550808ea7c5c9f1ba30eba376a.tar.gz
emacs-3a64a3cfa0f129550808ea7c5c9f1ba30eba376a.zip
(find-buffer-visiting): Accept new optional PREDICATE argument to return only a
buffer that satisfies the predicate. (insert-file-1): New function. (insert-file-literally): Use it. (insert-file): Use it.
-rw-r--r--lisp/files.el88
1 files changed, 48 insertions, 40 deletions
diff --git a/lisp/files.el b/lisp/files.el
index f050713e135..9d756bcfd29 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1047,40 +1047,44 @@ name to this list as a string."
1047 :type '(repeat (string :tag "Name")) 1047 :type '(repeat (string :tag "Name"))
1048 :group 'find-file) 1048 :group 'find-file)
1049 1049
1050(defun find-buffer-visiting (filename) 1050(defun find-buffer-visiting (filename &optional predicate)
1051 "Return the buffer visiting file FILENAME (a string). 1051 "Return the buffer visiting file FILENAME (a string).
1052This is like `get-file-buffer', except that it checks for any buffer 1052This is like `get-file-buffer', except that it checks for any buffer
1053visiting the same file, possibly under a different name. 1053visiting the same file, possibly under a different name.
1054If PREDICATE is non-nil, only a buffer satisfying it can be returned.
1054If there is no such live buffer, return nil." 1055If there is no such live buffer, return nil."
1055 (let ((buf (get-file-buffer filename)) 1056 (let ((predicate (or predicate #'identity))
1056 (truename (abbreviate-file-name (file-truename filename)))) 1057 (truename (abbreviate-file-name (file-truename filename))))
1057 (or buf 1058 (or (let ((buf (get-file-buffer filename)))
1058 (let ((list (buffer-list)) found) 1059 (when (and buf (funcall predicate buf)) buf))
1059 (while (and (not found) list) 1060 (let ((list (buffer-list)) found)
1060 (save-excursion 1061 (while (and (not found) list)
1061 (set-buffer (car list)) 1062 (save-excursion
1062 (if (and buffer-file-name 1063 (set-buffer (car list))
1063 (string= buffer-file-truename truename)) 1064 (if (and buffer-file-name
1064 (setq found (car list)))) 1065 (string= buffer-file-truename truename)
1065 (setq list (cdr list))) 1066 (funcall predicate (current-buffer)))
1066 found) 1067 (setq found (car list))))
1067 (let* ((attributes (file-attributes truename)) 1068 (setq list (cdr list)))
1068 (number (nthcdr 10 attributes)) 1069 found)
1069 (list (buffer-list)) found) 1070 (let* ((attributes (file-attributes truename))
1070 (and buffer-file-numbers-unique 1071 (number (nthcdr 10 attributes))
1071 number 1072 (list (buffer-list)) found)
1072 (while (and (not found) list) 1073 (and buffer-file-numbers-unique
1073 (with-current-buffer (car list) 1074 number
1074 (if (and buffer-file-name 1075 (while (and (not found) list)
1075 (equal buffer-file-number number) 1076 (with-current-buffer (car list)
1076 ;; Verify this buffer's file number 1077 (if (and buffer-file-name
1077 ;; still belongs to its file. 1078 (equal buffer-file-number number)
1078 (file-exists-p buffer-file-name) 1079 ;; Verify this buffer's file number
1079 (equal (file-attributes buffer-file-truename) 1080 ;; still belongs to its file.
1080 attributes)) 1081 (file-exists-p buffer-file-name)
1081 (setq found (car list)))) 1082 (equal (file-attributes buffer-file-truename)
1082 (setq list (cdr list)))) 1083 attributes)
1083 found)))) 1084 (funcall predicate (current-buffer)))
1085 (setq found (car list))))
1086 (setq list (cdr list))))
1087 found))))
1084 1088
1085(defcustom find-file-wildcards t 1089(defcustom find-file-wildcards t
1086 "*Non-nil means file-visiting commands should handle wildcards. 1090 "*Non-nil means file-visiting commands should handle wildcards.
@@ -1335,6 +1339,18 @@ This function ensures that none of these modifications will take place."
1335 (fset 'find-buffer-file-type find-buffer-file-type-function) 1339 (fset 'find-buffer-file-type find-buffer-file-type-function)
1336 (fmakunbound 'find-buffer-file-type))))) 1340 (fmakunbound 'find-buffer-file-type)))))
1337 1341
1342(defun insert-file-1 (filename insert-func)
1343 (if (file-directory-p filename)
1344 (signal 'file-error (list "Opening input file" "file is a directory"
1345 filename)))
1346 (let* ((buffer (find-buffer-visiting (abbreviate-file-name (file-truename filename))
1347 #'buffer-modified-p))
1348 (tem (funcall insert-func filename)))
1349 (push-mark (+ (point) (car (cdr tem))))
1350 (when buffer
1351 (message "File %s already visited and modified in buffer %s"
1352 filename (buffer-name buffer)))))
1353
1338(defun insert-file-literally (filename) 1354(defun insert-file-literally (filename)
1339 "Insert contents of file FILENAME into buffer after point with no conversion. 1355 "Insert contents of file FILENAME into buffer after point with no conversion.
1340 1356
@@ -1342,11 +1358,7 @@ This function is meant for the user to run interactively.
1342Don't call it from programs! Use `insert-file-contents-literally' instead. 1358Don't call it from programs! Use `insert-file-contents-literally' instead.
1343\(Its calling sequence is different; see its documentation)." 1359\(Its calling sequence is different; see its documentation)."
1344 (interactive "*fInsert file literally: ") 1360 (interactive "*fInsert file literally: ")
1345 (if (file-directory-p filename) 1361 (insert-file-1 filename #'insert-file-contents-literally))
1346 (signal 'file-error (list "Opening input file" "file is a directory"
1347 filename)))
1348 (let ((tem (insert-file-contents-literally filename)))
1349 (push-mark (+ (point) (car (cdr tem))))))
1350 1362
1351(defvar find-file-literally nil 1363(defvar find-file-literally nil
1352 "Non-nil if this buffer was made by `find-file-literally' or equivalent. 1364 "Non-nil if this buffer was made by `find-file-literally' or equivalent.
@@ -3147,11 +3159,7 @@ This function is meant for the user to run interactively.
3147Don't call it from programs! Use `insert-file-contents' instead. 3159Don't call it from programs! Use `insert-file-contents' instead.
3148\(Its calling sequence is different; see its documentation)." 3160\(Its calling sequence is different; see its documentation)."
3149 (interactive "*fInsert file: ") 3161 (interactive "*fInsert file: ")
3150 (if (file-directory-p filename) 3162 (insert-file-1 filename #'insert-file-contents))
3151 (signal 'file-error (list "Opening input file" "file is a directory"
3152 filename)))
3153 (let ((tem (insert-file-contents filename)))
3154 (push-mark (+ (point) (car (cdr tem))))))
3155 3163
3156(defun append-to-file (start end filename) 3164(defun append-to-file (start end filename)
3157 "Append the contents of the region to the end of file FILENAME. 3165 "Append the contents of the region to the end of file FILENAME.