Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created April 21, 2022 19:08
Show Gist options
  • Save rummelonp/ea410a77be4cd29532b2ccf652356900 to your computer and use it in GitHub Desktop.
Save rummelonp/ea410a77be4cd29532b2ccf652356900 to your computer and use it in GitHub Desktop.
neotree.el をなんか良い感じに動かすやつ
;; ← 押した時
;;
;; ファイルだったら: 前の行に移動
;; 開いたディレクトリだったら: 閉じる
;; 閉じたディレクトリだったら: 上の階層に移動
(defun neotree-left (arg)
(interactive "P")
(let* ((path (neo-buffer--get-filename-current-line))
(file-p (and path (not (file-directory-p path)))))
(cond (file-p
(neotree-previous-line))
((neo-buffer--expanded-node-p path)
(neo-buffer--set-expand path nil)
(neo-buffer--refresh t))
(t
(neotree-select-up-node)))))
;; → 押した時
;;
;; 閉じたディレクトリだったら: 開く
;; それ以外: 次の行に移動
(defun neotree-right (arg)
(interactive "P")
(let* ((path (neo-buffer--get-filename-current-line))
(file-p (and path (not (file-directory-p path)))))
(cond (file-p
(neotree-next-line))
((not (neo-buffer--expanded-node-p path))
(neo-buffer--set-expand path t)
(neo-buffer--refresh t))
(t
(neotree-select-down-node)))))
(define-key neotree-mode-map (kbd "<left>") 'neotree-left)
(define-key neotree-mode-map (kbd "<right>") 'neotree-right)
;; 良い感じに neotree 開いたり閉じたりするやつ
;;
;; neotree の窓にいる時: neotree 閉じる
;; neotree の窓にいない時:
;; プロジェクト内のファイルの時: プロジェクトを開きつつファイルにフォーカス
;; プロジェクト外のファイルの時: そのファイルのあるディレクトリを開く
(defun neotree-toggle-current ()
(interactive)
(let* ((path (or buffer-file-name (expand-file-name default-directory)))
(project (cdr (project-current nil path))))
(if (eq (selected-window) (neo-global--get-window))
(neotree-hide)
(progn
(if project
(progn
(neotree-dir project)
(neotree-find path))
(neo-global--open))
(neo-global--select-window)))))
;; 良い感じに窓移動するやつ
;;
;; 窓がひとつの場合: 窓水平分割して移動
;; 次の窓が neotree の場合: neotree の窓をスキップして更に次の窓に移動
;; それ以外: 次の窓に移動
(defun next-window-or-split-horizontally ()
(interactive)
(let* ((win-len (length (window-list)))
(current-win (selected-window))
(neotree-win (neo-global--get-window))
(next-win (next-window)))
(cond ((or (eq win-len 1)
(and neotree-win
(not (eq current-win neotree-win))
(eq win-len 2)))
(split-window-horizontally)
(other-window 1))
((eq next-win neotree-win)
(other-window 2))
(t
(other-window 1)))))
(define-key global-map (kbd "C-x C-d") 'neotree-toggle-current)
(define-key global-map (kbd "C-t") 'neotree-toggle-current)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment