2019-10-09 13:13:56 +02:00
|
|
|
;;; bookmark.el --- Saved files and directories on my filesystem -*- lexical-binding: t -*-
|
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; After enjoying and relying on Emacs's builtin `jump-to-register' command, I'd
|
|
|
|
;; like to recreate this functionality with a few extensions.
|
|
|
|
;;
|
|
|
|
;; Everything herein will mimmick my previous KBDs for `jump-to-register', which
|
|
|
|
;; were <leader>-j-<register-kbd>. If the `bookmark-path' is a file, Emacs will
|
|
|
|
;; open a buffer with that file. If the `bookmark-path' is a directory, Emacs
|
|
|
|
;; will open an ivy window searching that directory.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2020-01-27 17:21:30 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'f)
|
|
|
|
(require 'buffer)
|
|
|
|
(require 'list)
|
|
|
|
(require 'string)
|
2020-01-23 15:55:22 +01:00
|
|
|
(require 'set)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Constants
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(cl-defstruct bookmark label path kbd)
|
|
|
|
|
2020-01-22 22:54:22 +01:00
|
|
|
(defconst bookmark/install-kbds? t
|
|
|
|
"When t, install keybindings.")
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; TODO: Consider hosting this function somewhere other than here, since it
|
|
|
|
;; feels useful above of the context of bookmarks.
|
|
|
|
;; TODO: Assess whether it'd be better to use the existing function:
|
|
|
|
;; `counsel-projectile-switch-project-action'. See the noise I made on GH for
|
|
|
|
;; more context: https://github.com/ericdanan/counsel-projectile/issues/137
|
2020-01-22 22:54:22 +01:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(defun bookmark/handle-directory-dwim (path)
|
|
|
|
"Open PATH as either a project directory or a regular directory.
|
|
|
|
If PATH is `projectile-project-p', open with `counsel-projectile-find-file'.
|
|
|
|
Otherwise, open with `counsel-find-file'."
|
|
|
|
(if (projectile-project-p path)
|
|
|
|
(with-temp-buffer
|
|
|
|
(cd (projectile-project-p path))
|
|
|
|
(call-interactively #'counsel-projectile-find-file))
|
|
|
|
(let ((ivy-extra-directories nil))
|
|
|
|
(counsel-find-file path))))
|
|
|
|
|
|
|
|
(defconst bookmark/handle-directory #'bookmark/handle-directory-dwim
|
|
|
|
"Function to call when a bookmark points to a directory.")
|
|
|
|
|
|
|
|
(defconst bookmark/handle-file #'counsel-find-file-action
|
|
|
|
"Function to call when a bookmark points to a file.")
|
|
|
|
|
|
|
|
(defconst bookmark/whitelist
|
|
|
|
(list
|
2020-01-23 15:53:43 +01:00
|
|
|
(make-bookmark :label "depot"
|
|
|
|
:path "~/depot"
|
2020-01-22 22:53:43 +01:00
|
|
|
:kbd "t")
|
2019-10-09 13:13:56 +02:00
|
|
|
(make-bookmark :label "org"
|
|
|
|
:path "~/Dropbox/org"
|
|
|
|
:kbd "o")
|
2020-01-27 17:21:30 +01:00
|
|
|
(make-bookmark :label "universe"
|
|
|
|
:path "~/universe"
|
2020-01-22 22:53:43 +01:00
|
|
|
:kbd "m")
|
2019-10-09 13:13:56 +02:00
|
|
|
(make-bookmark :label "dotfiles"
|
2020-01-15 16:57:05 +01:00
|
|
|
:path "~/dotfiles"
|
2019-10-09 13:13:56 +02:00
|
|
|
:kbd "d")
|
|
|
|
(make-bookmark :label "current project"
|
|
|
|
:path constants/current-project
|
|
|
|
:kbd "p"))
|
|
|
|
"List of registered bookmarks.")
|
|
|
|
|
2020-01-22 22:55:07 +01:00
|
|
|
(defun bookmark/from-label (label)
|
|
|
|
"Return the bookmark with LABEL or nil."
|
|
|
|
(->> bookmark/whitelist
|
|
|
|
(list/find (lambda (b) (equal label (bookmark-label b))))))
|
|
|
|
|
2020-01-22 22:55:33 +01:00
|
|
|
(defun bookmark/magit-status ()
|
|
|
|
"Use ivy to select a bookmark and jump to its `magit-status' buffer."
|
|
|
|
(interactive)
|
2020-01-27 17:21:30 +01:00
|
|
|
(let ((labels (set/new "dotfiles" "universe" "depot"))
|
2020-01-23 15:55:22 +01:00
|
|
|
(all-labels (->> bookmark/whitelist
|
|
|
|
(list/map (>> bookmark-label))
|
|
|
|
set/from-list)))
|
|
|
|
(prelude/assert (set/subset? labels all-labels))
|
|
|
|
(ivy-read "Repository: "
|
|
|
|
(set/to-list labels)
|
|
|
|
:require-match t
|
|
|
|
:action (lambda (label)
|
|
|
|
(->> label
|
|
|
|
bookmark/from-label
|
|
|
|
bookmark-path
|
|
|
|
magit-status)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Consider `ivy-read' extension that takes a list of structs,
|
|
|
|
;; `struct-to-label' and `label-struct' functions.
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; API
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defun bookmark/open (b)
|
|
|
|
"Open bookmark, B, in a new buffer or an ivy minibuffer."
|
|
|
|
(let ((path (bookmark-path b)))
|
|
|
|
(cond
|
|
|
|
((f-directory? path)
|
|
|
|
(funcall bookmark/handle-directory path))
|
|
|
|
((f-file? path)
|
|
|
|
(funcall bookmark/handle-file path)))))
|
|
|
|
|
|
|
|
(defun bookmark/ivy-open ()
|
|
|
|
"Use ivy to filter available bookmarks."
|
|
|
|
(interactive)
|
|
|
|
(ivy-read "Bookmark: "
|
|
|
|
(->> bookmark/whitelist
|
|
|
|
(list/map #'bookmark-label))
|
|
|
|
:require-match t
|
|
|
|
:action (lambda (label)
|
2020-01-22 22:55:07 +01:00
|
|
|
(bookmark/open (bookmark/from-label label)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(when bookmark/install-kbds?
|
2020-01-18 23:52:59 +01:00
|
|
|
(general-define-key
|
|
|
|
:prefix "<SPC>"
|
|
|
|
:states '(normal)
|
|
|
|
"jj" #'bookmark/ivy-open)
|
2019-10-09 13:13:56 +02:00
|
|
|
(->> bookmark/whitelist
|
|
|
|
(list/map
|
|
|
|
(lambda (b)
|
2020-01-18 23:52:59 +01:00
|
|
|
(general-define-key
|
|
|
|
:prefix "<SPC>"
|
|
|
|
:states '(normal)
|
|
|
|
(string/concat "j" (bookmark-kbd b))
|
|
|
|
;; TODO: Consider `cl-labels' so `which-key' minibuffer is more
|
|
|
|
;; helpful.
|
2020-01-22 22:55:33 +01:00
|
|
|
(lambda () (interactive) (bookmark/open b))))))
|
|
|
|
(general-define-key
|
|
|
|
:states '(normal)
|
|
|
|
:prefix "<SPC>"
|
|
|
|
"gS" #'bookmark/magit-status))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'bookmark)
|
|
|
|
;;; bookmark.el ends here
|