ranger.el icon indicating copy to clipboard operation
ranger.el copied to clipboard

Add customized directory to `ranger-go`?

Open yanghaoxie opened this issue 6 years ago • 3 comments

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?

yanghaoxie avatar May 30 '18 11:05 yanghaoxie

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!

japhir avatar Aug 10 '18 08:08 japhir

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)

yanghaoxie avatar Aug 10 '18 08:08 yanghaoxie

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

plchldr avatar Jan 13 '19 23:01 plchldr