;;; $Header: /local/repository/cvsroot/web-seiten/emacs/complete-fn.el,v 1.2 2003/12/23 07:18:54 ni Exp $ ;;; Author: nickel@cs.tu-berlin.de ;;; complete-fn.el ;;; The command complete-filename lets you use filename completion on ;;; a filename the cursor is at or after in a text buffer. ;;; complete-filename tries to guess what text in the buffer already ;;; belongs to a filename. file-name-boundary-regexp is used to find ;;; possible delimiters of the filename. (defvar file-name-boundary-regexp "[]\n\t |&;:\\\\\\$*?()['\"`<>]" "Regexp matching characters that are supposed not to occur in filenames.") (defun complete-file-name (arg) "Complete filename point is at or after using the minibuffer. With one C-u, use the value of mark as the beginning of the filename. With two C-u's, don't grab an initial string from the buffer. Any other prefix argument is ignored." (interactive "P") (let* ((grab-from (if (equal arg '(4)) (mark-marker))) (dont-grab (equal arg '(16))) (file-name (if dont-grab "" (grab-file-name nil grab-from))) (template (complete-file-name-internal file-name nil nil 'use-it)) (completion (completing-read "Complete Filename: " 'complete-file-name-internal nil nil template))) (if (equal file-name completion) (message "No completion") (or dont-grab (grab-file-name 'delete grab-from)) (insert completion)))) (defun complete-file-name-internal (file-name pred flag &optional use-it) "Internal subroutine for complete-file-name. Do not call this." (let ((dir (file-name-directory file-name)) (nondir (file-name-nondirectory file-name))) (cond ((null flag) (let ((completion (file-name-completion nondir (or dir "")))) (if (stringp completion) (concat dir completion) (if use-it file-name completion)))) ((eq flag t) (file-name-all-completions nondir (or dir ""))) ((eq flag 'lambda) (file-name-all-completions nondir (or dir "")))))) (defun grab-file-name (delete begin) "Return filename point is at or after. If DELETE is non-nil, delete the filename from the buffer. If BEGIN is non-nil, this is the beginning of the filename." (save-excursion (if (and (or (eobp) (looking-at file-name-boundary-regexp)) (or (bobp) (string-match file-name-boundary-regexp (buffer-substring (1- (point)) (point))))) "" (let* ((fnbeg (if begin begin (save-excursion (re-search-backward file-name-boundary-regexp (point-min) 'just-move) (if (looking-at file-name-boundary-regexp) (forward-char 1)) (point)))) (fnend (progn (re-search-forward file-name-boundary-regexp (point-max) 'just-move) (or (eobp) (forward-char -1)) (point)))) (prog1 (buffer-substring fnbeg fnend) (if delete (delete-region fnbeg fnend))))))) ;;; Local Variables: ;;; eval: (defun byte-compile-this-file () (write-region (point-min) (point-max) buffer-file-name nil 't) (byte-compile-file buffer-file-name) nil) ;;; write-file-hooks: (byte-compile-this-file) ;;; End: