punassuming / ranger.el

Bringing the goodness of ranger to dired!
699 stars 52 forks source link

Add customized directory to `ranger-go`? #183

Open yanghaoxie opened 6 years ago

yanghaoxie commented 6 years ago

I did not find a defcustom to customize this feature, I used advice-add to modify this function a little. So, is there any official way to do this?

japhir commented 6 years ago

Could you share how you do it? I'd like to create shortcut keys to my commonly accessed directories and was just looking at the code to see which list I should be appending them to.

In general I think it would be great if this was a customisable list!

yanghaoxie commented 6 years ago

I am a newbie, so the little hack may be dirty. ELisp has a feature called advicing https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html, I just override the origin ranger-go function with my/ranger-go which add more entries. The code is pretty simple, it explain itself. BTW, you said you can just append code to list? Can you show me? This could make the hack more elegant.

(defun my/ranger-go (path)
  "Go subroutine"
  (interactive
   (list
    (read-char-choice
     "e   : /etc
u   : /usr
d   : ~/Downloads
b   : ~/Dropbox
l   : follow directory link
L   : follow selected file
o   : /opt
v   : /var
h   : ~/
m   : /media
M   : /mnt
s   : /srv
r,/ : /
R   : ranger . el location
> "
     '(?q ?e ?u ?d ?b ?l ?L ?o ?v ?m ?M ?s ?r ?R ?/ ?h ?g ?D ?j ?k ?T ?t ?n ?c))))
  (message nil)
  (let* ((c (char-to-string path))
         (new-path
          (cl-case (intern c)
            ('e "/etc")
            ('u "/usr")
            ('d "~/Downloads")
        ('b "~/Dropbox")
            ('l (file-truename default-directory))
            ('L (file-truename (dired-get-filename)))
            ('o "/opt")
            ('v "/var")
            ('m "/media")
            ('M "/mnt")
            ('s "/srv")
            ('r "/")
            ('R (file-truename (file-name-directory (find-library-name "ranger.el"))))
            ('h  "~/")
            ('/ "/")))
         (alt-option
          (cl-case (intern c)
            ;; Subdir Handlng
            ('j 'ranger-next-subdir)
            ('k 'ranger-prev-subdir)
            ;; Tab Handling
            ('n 'ranger-new-tab)
            ('T 'ranger-prev-tab)
            ('t 'ranger-next-tab)
            ('c 'ranger-close-tab)
            ('g 'ranger-goto-top))))
    (when (string-equal c "q")
      (keyboard-quit))
    (when (and new-path (file-directory-p new-path))
      (ranger-find-file new-path))
    (when (eq system-type 'windows-nt)
      (when (string-equal c "D")
        (ranger-show-drives)))
    (when alt-option
      (call-interactively alt-option))))
(advice-add 'ranger-go :override #'my/ranger-go)
plchldr commented 5 years ago

Changing default paths is also relevant for distros where it's /run/media/$USER instead of /media for example.