comparison lisp/minibuffer.el @ 94300:b47239d57d36

* lisp/minibuffer.el (read-file-name-function, read-file-name-predicate) (read-file-name-completion-ignore-case, insert-default-directory): New vars, moved from fileio.c. (read-file-name): New fun, moved from fileio.c. * lisp/cus-start.el: Remove insert-default-directory and read-file-name-completion-ignore-case. * src/fileio.c (Vread_file_name_function, Vread_file_name_predicate) (read_file_name_completion_ignore_case, insert_default_directory) (Qdefault_directory): Move to minibuffer.el. (Fread_file_name): Call the new `read-file-name' instead.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 23 Apr 2008 18:19:57 +0000
parents 57c8f6a6456a
children 9060af7294b9
comparison
equal deleted inserted replaced
94299:c4b844d3df2c 94300:b47239d57d36
762 (defalias 'read-file-name-internal 762 (defalias 'read-file-name-internal
763 (completion-table-in-turn 'completion--embedded-envvar-table 763 (completion-table-in-turn 'completion--embedded-envvar-table
764 'completion--file-name-table) 764 'completion--file-name-table)
765 "Internal subroutine for `read-file-name'. Do not call this.") 765 "Internal subroutine for `read-file-name'. Do not call this.")
766 766
767 (defvar read-file-name-function nil
768 "If this is non-nil, `read-file-name' does its work by calling this function.")
769
770 (defvar read-file-name-predicate nil
771 "Current predicate used by `read-file-name-internal'.")
772
773 (defcustom read-file-name-completion-ignore-case
774 (if (memq system-type '(ms-dos windows-nt darwin macos vax-vms axp-vms))
775 t nil)
776 "Non-nil means when reading a file name completion ignores case."
777 :group 'minibuffer
778 :type 'boolean
779 :version "22.1")
780
781 (defcustom insert-default-directory t
782 "Non-nil means when reading a filename start with default dir in minibuffer.
783
784 When the initial minibuffer contents show a name of a file or a directory,
785 typing RETURN without editing the initial contents is equivalent to typing
786 the default file name.
787
788 If this variable is non-nil, the minibuffer contents are always
789 initially non-empty, and typing RETURN without editing will fetch the
790 default name, if one is provided. Note however that this default name
791 is not necessarily the same as initial contents inserted in the minibuffer,
792 if the initial contents is just the default directory.
793
794 If this variable is nil, the minibuffer often starts out empty. In
795 that case you may have to explicitly fetch the next history element to
796 request the default name; typing RETURN without editing will leave
797 the minibuffer empty.
798
799 For some commands, exiting with an empty minibuffer has a special meaning,
800 such as making the current buffer visit no file in the case of
801 `set-visited-file-name'."
802 :group 'minibuffer
803 :type 'boolean)
804
805 (defun read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
806 "Read file name, prompting with PROMPT and completing in directory DIR.
807 Value is not expanded---you must call `expand-file-name' yourself.
808 Default name to DEFAULT-FILENAME if user exits the minibuffer with
809 the same non-empty string that was inserted by this function.
810 (If DEFAULT-FILENAME is omitted, the visited file name is used,
811 except that if INITIAL is specified, that combined with DIR is used.)
812 If the user exits with an empty minibuffer, this function returns
813 an empty string. (This can only happen if the user erased the
814 pre-inserted contents or if `insert-default-directory' is nil.)
815 Fourth arg MUSTMATCH non-nil means require existing file's name.
816 Non-nil and non-t means also require confirmation after completion.
817 Fifth arg INITIAL specifies text to start with.
818 If optional sixth arg PREDICATE is non-nil, possible completions and
819 the resulting file name must satisfy (funcall PREDICATE NAME).
820 DIR should be an absolute directory name. It defaults to the value of
821 `default-directory'.
822
823 If this command was invoked with the mouse, use a file dialog box if
824 `use-dialog-box' is non-nil, and the window system or X toolkit in use
825 provides a file dialog box.
826
827 See also `read-file-name-completion-ignore-case'
828 and `read-file-name-function'."
829 (unless dir (setq dir default-directory))
830 (unless (file-name-absolute-p dir) (setq dir (expand-file-name dir)))
831 (unless default-filename
832 (setq default-filename (if initial (expand-file-name initial dir)
833 buffer-file-name)))
834 ;; If dir starts with user's homedir, change that to ~.
835 (setq dir (abbreviate-file-name dir))
836 ;; Likewise for default-filename.
837 (setq default-filename (abbreviate-file-name default-filename))
838 (let ((insdef (cond
839 ((and insert-default-directory (stringp dir))
840 (if initial
841 (cons (minibuffer--double-dollars (concat dir initial))
842 (length (minibuffer--double-dollars dir)))
843 (minibuffer--double-dollars dir)))
844 (initial (cons (minibuffer--double-dollars initial) 0)))))
845
846 (if read-file-name-function
847 (funcall read-file-name-function
848 prompt dir default-filename mustmatch initial predicate)
849 (let ((default-directory (file-name-as-directory (expand-file-name dir)))
850 (completion-ignore-case read-file-name-completion-ignore-case)
851 (minibuffer-completing-file-name t)
852 (read-file-name-predicate (or predicate 'file-exists-p))
853 (add-to-history nil))
854
855 (let* ((val
856 (if (not (next-read-file-uses-dialog-p))
857 (completing-read prompt 'read-file-name-internal
858 nil mustmatch insdef 'file-name-history
859 default-filename)
860 ;; If DIR contains a file name, split it.
861 (let ((file (file-name-nondirectory dir)))
862 (when (and default-filename (not (zerop (length file))))
863 (setq default-filename file)
864 (setq dir (file-name-directory dir)))
865 (if default-filename
866 (setq default-filename
867 (expand-file-name default-filename dir)))
868 (setq add-to-history t)
869 (x-file-dialog prompt dir default-filename mustmatch
870 (eq predicate 'file-directory-p)))))
871
872 (replace-in-history (eq (car-safe file-name-history) val)))
873 ;; If completing-read returned the inserted default string itself
874 ;; (rather than a new string with the same contents),
875 ;; it has to mean that the user typed RET with the minibuffer empty.
876 ;; In that case, we really want to return ""
877 ;; so that commands such as set-visited-file-name can distinguish.
878 (when (eq val default-filename)
879 ;; In this case, completing-read has not added an element
880 ;; to the history. Maybe we should.
881 (if (not replace-in-history)
882 (setq add-to-history t))
883 (setq val ""))
884 (unless val (error "No file name specified"))
885
886 (if (and default-filename
887 (string-equal val (if (consp insdef) (car insdef) insdef)))
888 (setq val default-filename))
889 (setq val (substitute-in-file-name val))
890
891 (if replace-in-history
892 ;; Replace what Fcompleting_read added to the history
893 ;; with what we will actually return.
894 (let ((val1 (minibuffer--double-dollars val)))
895 (if history-delete-duplicates
896 (setcdr file-name-history
897 (delete val1 (cdr file-name-history))))
898 (setcar file-name-history val1))
899 (if add-to-history
900 ;; Add the value to the history--but not if it matches
901 ;; the last value already there.
902 (let ((val1 (minibuffer--double-dollars val)))
903 (unless (and (consp file-name-history)
904 (equal (car file-name-history) val1))
905 (setq file-name-history
906 (cons val1
907 (if history-delete-duplicates
908 (delete val1 file-name-history)
909 file-name-history)))))))
910 val)))))
911
767 (defun internal-complete-buffer-except (&optional buffer) 912 (defun internal-complete-buffer-except (&optional buffer)
768 "Perform completion on all buffers excluding BUFFER. 913 "Perform completion on all buffers excluding BUFFER.
769 Like `internal-complete-buffer', but removes BUFFER from the completion list." 914 Like `internal-complete-buffer', but removes BUFFER from the completion list."
770 (lexical-let ((except (if (stringp buffer) buffer (buffer-name buffer)))) 915 (lexical-let ((except (if (stringp buffer) buffer (buffer-name buffer))))
771 (apply-partially 'completion-table-with-predicate 916 (apply-partially 'completion-table-with-predicate