aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2012-07-04 11:59:12 -0400
committerStefan Monnier2012-07-04 11:59:12 -0400
commit0781098af7c8da77b1d044dce151e6a130eb1e77 (patch)
treeb54ba05fbc92bdaaf889a445fe18d4edad8469da
parent3884d954f32acb816332d7837fe813bc546f6268 (diff)
downloademacs-0781098af7c8da77b1d044dce151e6a130eb1e77.tar.gz
emacs-0781098af7c8da77b1d044dce151e6a130eb1e77.zip
* lisp/files.el (locate-dominating-file): Allow `name' to be a predicate.
(find-file--read-only): New function. (find-file-read-only, find-file-read-only-other-window) (find-file-read-only-other-frame): Use it. (insert-file-contents-literally): Don't `fset'. (get-free-disk-space): Use locate-dominating-file.
-rw-r--r--lisp/ChangeLog7
-rw-r--r--lisp/files.el81
2 files changed, 38 insertions, 50 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 34a74656415..0a486daa809 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,12 @@
12012-07-04 Stefan Monnier <monnier@iro.umontreal.ca> 12012-07-04 Stefan Monnier <monnier@iro.umontreal.ca>
2 2
3 * files.el (locate-dominating-file): Allow `name' to be a predicate.
4 (find-file--read-only): New function.
5 (find-file-read-only, find-file-read-only-other-window)
6 (find-file-read-only-other-frame): Use it.
7 (insert-file-contents-literally): Don't `fset'.
8 (get-free-disk-space): Use locate-dominating-file.
9
3 * emacs-lisp/bytecomp.el (byte-compile): Don't signal an error if the 10 * emacs-lisp/bytecomp.el (byte-compile): Don't signal an error if the
4 function is already compiled. 11 function is already compiled.
5 12
diff --git a/lisp/files.el b/lisp/files.el
index 2b5717a719c..34144f494cf 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -876,12 +876,12 @@ or mount points potentially requiring authentication as a different user.")
876;; nil))) 876;; nil)))
877 877
878(defun locate-dominating-file (file name) 878(defun locate-dominating-file (file name)
879 "Look up the directory hierarchy from FILE for a file named NAME. 879 "Look up the directory hierarchy from FILE for a directory containing NAME.
880Stop at the first parent directory containing a file NAME, 880Stop at the first parent directory containing a file NAME,
881and return the directory. Return nil if not found. 881and return the directory. Return nil if not found.
882 882Instead of a string, NAME can also be a predicate taking one argument
883This function only tests if FILE exists. If you care about whether 883\(a directory) and returning a non-nil value if that directory is the one for
884it is readable, regular, etc., you should test the result." 884which we're looking."
885 ;; We used to use the above locate-dominating-files code, but the 885 ;; We used to use the above locate-dominating-files code, but the
886 ;; directory-files call is very costly, so we're much better off doing 886 ;; directory-files call is very costly, so we're much better off doing
887 ;; multiple calls using the code in here. 887 ;; multiple calls using the code in here.
@@ -908,16 +908,14 @@ it is readable, regular, etc., you should test the result."
908 ;; (setq user (nth 2 (file-attributes file))) 908 ;; (setq user (nth 2 (file-attributes file)))
909 ;; (and prev-user (not (equal user prev-user)))) 909 ;; (and prev-user (not (equal user prev-user))))
910 (string-match locate-dominating-stop-dir-regexp file))) 910 (string-match locate-dominating-stop-dir-regexp file)))
911 ;; FIXME? maybe this function should (optionally?) 911 (setq try (if (stringp name)
912 ;; use file-readable-p instead. In many cases, an unreadable 912 (file-exists-p (expand-file-name name file))
913 ;; FILE is no better than a non-existent one. 913 (funcall name file)))
914 ;; See eg dir-locals-find-file.
915 (setq try (file-exists-p (expand-file-name name file)))
916 (cond (try (setq root file)) 914 (cond (try (setq root file))
917 ((equal file (setq file (file-name-directory 915 ((equal file (setq file (file-name-directory
918 (directory-file-name file)))) 916 (directory-file-name file))))
919 (setq file nil)))) 917 (setq file nil))))
920 root)) 918 (if root (file-name-as-directory root))))
921 919
922 920
923(defun executable-find (command) 921(defun executable-find (command)
@@ -1467,23 +1465,26 @@ file names with wildcards."
1467 (find-file filename) 1465 (find-file filename)
1468 (current-buffer))) 1466 (current-buffer)))
1469 1467
1470(defun find-file-read-only (filename &optional wildcards) 1468(defun find-file--read-only (fun filename wildcards)
1471 "Edit file FILENAME but don't allow changes.
1472Like \\[find-file], but marks buffer as read-only.
1473Use \\[toggle-read-only] to permit editing."
1474 (interactive
1475 (find-file-read-args "Find file read-only: "
1476 (confirm-nonexistent-file-or-buffer)))
1477 (unless (or (and wildcards find-file-wildcards 1469 (unless (or (and wildcards find-file-wildcards
1478 (not (string-match "\\`/:" filename)) 1470 (not (string-match "\\`/:" filename))
1479 (string-match "[[*?]" filename)) 1471 (string-match "[[*?]" filename))
1480 (file-exists-p filename)) 1472 (file-exists-p filename))
1481 (error "%s does not exist" filename)) 1473 (error "%s does not exist" filename))
1482 (let ((value (find-file filename wildcards))) 1474 (let ((value (funcall fun filename wildcards)))
1483 (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1))) 1475 (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
1484 (if (listp value) value (list value))) 1476 (if (listp value) value (list value)))
1485 value)) 1477 value))
1486 1478
1479(defun find-file-read-only (filename &optional wildcards)
1480 "Edit file FILENAME but don't allow changes.
1481Like \\[find-file], but marks buffer as read-only.
1482Use \\[toggle-read-only] to permit editing."
1483 (interactive
1484 (find-file-read-args "Find file read-only: "
1485 (confirm-nonexistent-file-or-buffer)))
1486 (find-file--read-only #'find-file filename wildcards))
1487
1487(defun find-file-read-only-other-window (filename &optional wildcards) 1488(defun find-file-read-only-other-window (filename &optional wildcards)
1488 "Edit file FILENAME in another window but don't allow changes. 1489 "Edit file FILENAME in another window but don't allow changes.
1489Like \\[find-file-other-window], but marks buffer as read-only. 1490Like \\[find-file-other-window], but marks buffer as read-only.
@@ -1491,15 +1492,7 @@ Use \\[toggle-read-only] to permit editing."
1491 (interactive 1492 (interactive
1492 (find-file-read-args "Find file read-only other window: " 1493 (find-file-read-args "Find file read-only other window: "
1493 (confirm-nonexistent-file-or-buffer))) 1494 (confirm-nonexistent-file-or-buffer)))
1494 (unless (or (and wildcards find-file-wildcards 1495 (find-file--read-only #'find-file-other-window filename wildcards))
1495 (not (string-match "\\`/:" filename))
1496 (string-match "[[*?]" filename))
1497 (file-exists-p filename))
1498 (error "%s does not exist" filename))
1499 (let ((value (find-file-other-window filename wildcards)))
1500 (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
1501 (if (listp value) value (list value)))
1502 value))
1503 1496
1504(defun find-file-read-only-other-frame (filename &optional wildcards) 1497(defun find-file-read-only-other-frame (filename &optional wildcards)
1505 "Edit file FILENAME in another frame but don't allow changes. 1498 "Edit file FILENAME in another frame but don't allow changes.
@@ -1508,15 +1501,7 @@ Use \\[toggle-read-only] to permit editing."
1508 (interactive 1501 (interactive
1509 (find-file-read-args "Find file read-only other frame: " 1502 (find-file-read-args "Find file read-only other frame: "
1510 (confirm-nonexistent-file-or-buffer))) 1503 (confirm-nonexistent-file-or-buffer)))
1511 (unless (or (and wildcards find-file-wildcards 1504 (find-file--read-only #'find-file-other-frame filename wildcards))
1512 (not (string-match "\\`/:" filename))
1513 (string-match "[[*?]" filename))
1514 (file-exists-p filename))
1515 (error "%s does not exist" filename))
1516 (let ((value (find-file-other-frame filename wildcards)))
1517 (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
1518 (if (listp value) value (list value)))
1519 value))
1520 1505
1521(defun find-alternate-file-other-window (filename &optional wildcards) 1506(defun find-alternate-file-other-window (filename &optional wildcards)
1522 "Find file FILENAME as a replacement for the file in the next window. 1507 "Find file FILENAME as a replacement for the file in the next window.
@@ -2020,6 +2005,8 @@ Do you want to revisit the file normally now? ")
2020 (after-find-file error (not nowarn))) 2005 (after-find-file error (not nowarn)))
2021 (current-buffer)))) 2006 (current-buffer))))
2022 2007
2008(defvar file-name-buffer-file-type-alist) ;From dos-w32.el.
2009
2023(defun insert-file-contents-literally (filename &optional visit beg end replace) 2010(defun insert-file-contents-literally (filename &optional visit beg end replace)
2024 "Like `insert-file-contents', but only reads in the file literally. 2011 "Like `insert-file-contents', but only reads in the file literally.
2025A buffer may be modified in several ways after reading into the buffer, 2012A buffer may be modified in several ways after reading into the buffer,
@@ -2031,21 +2018,14 @@ This function ensures that none of these modifications will take place."
2031 (after-insert-file-functions nil) 2018 (after-insert-file-functions nil)
2032 (coding-system-for-read 'no-conversion) 2019 (coding-system-for-read 'no-conversion)
2033 (coding-system-for-write 'no-conversion) 2020 (coding-system-for-write 'no-conversion)
2034 (find-buffer-file-type-function 2021 (file-name-buffer-file-type-alist '(("" . t)))
2035 (if (fboundp 'find-buffer-file-type)
2036 (symbol-function 'find-buffer-file-type)
2037 nil))
2038 (inhibit-file-name-handlers 2022 (inhibit-file-name-handlers
2023 ;; FIXME: Yuck!! We should turn insert-file-contents-literally
2024 ;; into a file operation instead!
2039 (append '(jka-compr-handler image-file-handler epa-file-handler) 2025 (append '(jka-compr-handler image-file-handler epa-file-handler)
2040 inhibit-file-name-handlers)) 2026 inhibit-file-name-handlers))
2041 (inhibit-file-name-operation 'insert-file-contents)) 2027 (inhibit-file-name-operation 'insert-file-contents))
2042 (unwind-protect 2028 (insert-file-contents filename visit beg end replace)))
2043 (progn
2044 (fset 'find-buffer-file-type (lambda (_filename) t))
2045 (insert-file-contents filename visit beg end replace))
2046 (if find-buffer-file-type-function
2047 (fset 'find-buffer-file-type find-buffer-file-type-function)
2048 (fmakunbound 'find-buffer-file-type)))))
2049 2029
2050(defun insert-file-1 (filename insert-func) 2030(defun insert-file-1 (filename insert-func)
2051 (if (file-directory-p filename) 2031 (if (file-directory-p filename)
@@ -5958,11 +5938,12 @@ returns nil."
5958 (when (and directory-free-space-program 5938 (when (and directory-free-space-program
5959 ;; Avoid failure if the default directory does 5939 ;; Avoid failure if the default directory does
5960 ;; not exist (Bug#2631, Bug#3911). 5940 ;; not exist (Bug#2631, Bug#3911).
5961 (let ((default-directory "/")) 5941 (let ((default-directory
5962 (eq (call-process directory-free-space-program 5942 (locate-dominating-file dir 'file-directory-p)))
5943 (eq (process-file directory-free-space-program
5963 nil t nil 5944 nil t nil
5964 directory-free-space-args 5945 directory-free-space-args
5965 dir) 5946 (file-relative-name dir))
5966 0))) 5947 0)))
5967 ;; Assume that the "available" column is before the 5948 ;; Assume that the "available" column is before the
5968 ;; "capacity" column. Find the "%" and scan backward. 5949 ;; "capacity" column. Find the "%" and scan backward.