Icicles - icicle-find-file Definition

iciclesimage
Previous: Icicles - Defining Icicles CommandsIcicles

Definition of Command `icicle-find-file'

Here is the complete definition of command ‘icicle-find-file’:

  (icicle-define-file-command
     icicle-find-file                             ; Command name
     "Visit a file or directory."                 ; Doc string
     icicle-find-file-w-wildcards                 ; Function to perform the action
     "File or directory: " nil default-directory) ; `read-file-name' arguments
  (defun icicle-find-file-w-wildcards (filename)
    "Find file FILENAME, where the name possibly includes shell wildcards."
    (find-file filename t))

Here is the resulting definition that is generated by that simple definition. This is the EmacsLisp source code; you would actually use the byte-compiled version of this. As you can see, even most of the doc string is generated.

(defun icicle-find-file ()
  "Visit a file or directory.
You can use `S-delete' during completion to delete a candidate file.

Read input, then call `icicle-find-file-w-wildcards' to act on it.

Input-candidate completion and cycling are available.  While cycling,
these keys with prefix `C-' are active:

`C-mouse-2', `C-RET' - Act on current completion candidate only
`C-down'  - Move to next prefix-completion candidate and act
`C-up'    - Move to previous prefix-completion candidate and act
`C-next'  - Move to next apropos-completion candidate and act
`C-prior' - Move to previous apropos-completion candidate and act
`C-!'     - Act on *all* candidates, successively (careful!)

When candidate action and cycling are combined (e.g. `C-next'), user
option `icicle-act-before-cycle-flag' determines which occurs first.

With prefix `C-M-' instead of `C-', the same keys (`C-M-mouse-2',
`C-M-RET', `C-M-down', and so on) provide help about candidates.

Use `mouse-2', `RET', or `S-RET' to finally choose a candidate, or
`C-g' to quit.

This is an Icicles command - see `icicle-mode'."
  (interactive)
  (let* ((orig-buff (current-buffer))
         (orig-window (selected-window))
         (icicle-delete-candidate-object 'icicle-delete-file-or-directory)
         (icicle-candidate-action-fn
          (lambda (candidate)
            (let ((minibuffer-completion-table minibuffer-completion-table)
                  (minibuffer-completion-predicate minibuffer-completion-predicate)
                  (minibuffer-completion-confirm minibuffer-completion-confirm)
                  (minibuffer-completing-file-name minibuffer-completing-file-name)
                  (minibuffer-completing-symbol
                   (and (boundp 'minibuffer-completing-symbol)
                        minibuffer-completing-symbol))
                  (minibuffer-exit-hook minibuffer-exit-hook)
                  (minibuffer-help-form minibuffer-help-form)
                  (minibuffer-history-variable minibuffer-history-variable)
                  (minibuffer-history-case-insensitive-variables
                   minibuffer-history-case-insensitive-variables)
                  (minibuffer-history-sexp-flag minibuffer-history-sexp-flag)
                  (minibuffer-message-timeout
                   (and (boundp 'minibuffer-message-timeout)
                        minibuffer-message-timeout))
                  (minibuffer-prompt-properties
                   (and (boundp 'minibuffer-prompt-properties)
                        minibuffer-prompt-properties))
                  (minibuffer-setup-hook minibuffer-setup-hook)
                  (minibuffer-text-before-history minibuffer-text-before-history))
              (setq candidate
                    (expand-file-name
                     candidate (file-name-directory icicle-last-input)))
              (condition-case in-action-fn
                  (cond ((and (buffer-live-p orig-buff) (window-live-p orig-window))
                         (with-current-buffer orig-buff
                           (save-selected-window
                             (select-window orig-window)
                             (funcall 'icicle-find-file-w-wildcards candidate))))
                        ((window-live-p orig-window)
                         (save-selected-window
                           (select-window orig-window)
                           (funcall 'icicle-find-file-w-wildcards candidate)))
                        (t
                         (funcall 'icicle-find-file-w-wildcards candidate)))
                (error
                 (unless (string= "Cannot switch buffers in minibuffer window"
                                  (error-message-string in-action-fn))
                   (error "%s" (error-message-string in-action-fn)))
                 (when (window-live-p orig-window)
                   (select-frame-set-input-focus (window-frame orig-window)))
                 (funcall 'icicle-find-file-w-wildcards candidate)))
              (select-frame-set-input-focus (window-frame (minibuffer-window)))
              nil))))
    nil
    (condition-case act-on-choice
        (let ((file-choice
               (if (< emacs-major-version 21)
                   (read-file-name "File or directory: " nil
                                   (if (and (eq major-mode 'dired-mode)
                                            (fboundp 'diredp-find-a-file))
                                       (condition-case nil
                                           (abbreviate-file-name
                                            (dired-get-file-for-visit))
                                         (error default-directory))
                                     default-directory)
                                   nil nil)
                 (read-file-name "File or directory: " nil
                                 (if (and (eq major-mode 'dired-mode)
                                          (fboundp 'diredp-find-a-file))
                                     (condition-case nil
                                         (abbreviate-file-name
                                          (dired-get-file-for-visit))
                                       (error default-directory))
                                   default-directory)
                                 nil nil nil))))
          (setq icicle-candidate-action-fn nil)
          (funcall 'icicle-find-file-w-wildcards file-choice))
      (quit (icicle-try-switch-buffer orig-buff) nil)
      (error (icicle-try-switch-buffer orig-buff)
             nil
             (error "%s" (error-message-string act-on-choice))))
    nil))

Thanks to library icicles-mac.el, you get the complex behavior defined by this command for free. Just use macros ‘icicle-define-command’ and ‘icicle-define-file-command’ to define robust multi-commands the easy way.


Previous: Icicles - Defining Icicles CommandsIcicles

DrewsElispLibraries referenced here: Lisp:icicles.el, Lisp:synonyms.el

CategoryCommands CategoryBufferSwitching CategoryCompletion CategoryRegexp CategoryDirectories CategoryFiles CategoryProgrammerUtils CategoryCode