Brady Trainor
Brady Trainor

Reputation: 2076

Emacs; C-x C-j before loading dired, dired-x

Was trying to reduce number of requires in my init.el.

However, in dired-x, there is the awesome C-x C-j = dired-jump, which jumps to the directory of current buffer. It is natural to want to use C-x C-j even if we haven't loaded dired and dired-x yet. But then it is undefined.

How to reconcile?

In other words, I run into this problem because of the following concoction I am experimenting with:

(setq-default dired-omit-files-p t)          ; The old way was messy
(setq dired-omit-files "^\\.?#\\|^\\.$")     ; I like `..' in my dired
(setq dired-details-hidden-string "")
(add-hook 'dired-load-hook
          (lambda ()
            (load "dired-x")
            (when (locate-library "dired-details")
              (load "dired-details")
              (dired-details-install)
              )
            ))

I wondered if there was a magic autoload for the function, though I don't really understand these. That is, in dired-x.el, I found

;;;###autoload
(defun dired-jump ...

But really, these are a little over my head, so I abandoned it.

I made a "simpler" solution, was going to delete this, but have a feeling that others will like this, so will make the question and answer for you. Comments and other strategies are appreciated.

Upvotes: 2

Views: 376

Answers (2)

phils
phils

Reputation: 73274

C-hig (dired-x) Optional Installation Dired Jump RET

In order to have dired-jump and dired-jump-other-window (*note Miscellaneous Commands::) work before dired and dired-x have been properly loaded you should set-up an autoload for these functions. In your .emacs file put

(autoload 'dired-jump "dired-x"
  "Jump to Dired buffer corresponding to current buffer." t)

(autoload 'dired-jump-other-window "dired-x"
  "Like \\[dired-jump] (dired-jump) but in other window." t)

(define-key global-map "\C-x\C-j" 'dired-jump)
(define-key global-map "\C-x4\C-j" 'dired-jump-other-window)

I'm not sure why the dired-x library remains separate from the default functionality. That ;;;###autoload cookie only has an effect if the Emacs build process looks at it (the autoload statements resulting from such comments generally end up in one of the loaddefs.el files), so clearly that's not happening (and presumably on purpose, but your guess is as good as mine as to why).

Upvotes: 3

Brady Trainor
Brady Trainor

Reputation: 2076

The following simple solution simply defines C-x C-j, but will be overwritten as soon as dired -and thus- dired-x are loaded.

;; if dired(-x) is not loaded, C-x C-j is undefined
(defun undefined-c-x-c-j-loads-dired-and-jumps ()
  (interactive)
  (load "dired")
  (dired-jump))
(global-set-key (kbd "C-x C-j") 'undefined-c-x-c-j-loads-dired-and-jumps)

Upvotes: 0

Related Questions