2019-10-09 13:13:56 +02:00
|
|
|
;;; bookmark.el --- Saved files and directories on my filesystem -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; 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)
|
2021-11-08 19:36:45 +01:00
|
|
|
(require 'dash)
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'string)
|
2020-01-23 15:55:22 +01:00
|
|
|
(require 'set)
|
2020-08-25 15:19:19 +02:00
|
|
|
(require 'constants)
|
2020-09-02 16:01:43 +02:00
|
|
|
(require 'general)
|
2021-12-24 18:11:54 +01:00
|
|
|
(require 'tvl)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Constants
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(cl-defstruct bookmark label path kbd)
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun bookmark-handle-directory-dwim (path)
|
2021-11-08 23:45:59 +01:00
|
|
|
"Open PATH as either a project directory or a regular directory."
|
|
|
|
(with-temp-buffer
|
|
|
|
(cd path)
|
|
|
|
(call-interactively #'project-find-file)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst bookmark-handle-directory #'bookmark-handle-directory-dwim
|
2019-10-09 13:13:56 +02:00
|
|
|
"Function to call when a bookmark points to a directory.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst bookmark-handle-file #'counsel-find-file-action
|
2019-10-09 13:13:56 +02:00
|
|
|
"Function to call when a bookmark points to a file.")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defconst bookmark-whitelist
|
2019-10-09 13:13:56 +02:00
|
|
|
(list
|
2021-12-24 00:30:31 +01:00
|
|
|
(make-bookmark :label "depot"
|
2021-12-24 18:11:54 +01:00
|
|
|
:path tvl-depot-path
|
2021-12-24 00:30:31 +01:00
|
|
|
:kbd "d"))
|
2019-10-09 13:13:56 +02:00
|
|
|
"List of registered bookmarks.")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; API
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun bookmark-open (b)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Open bookmark, B, in a new buffer or an ivy minibuffer."
|
|
|
|
(let ((path (bookmark-path b)))
|
|
|
|
(cond
|
|
|
|
((f-directory? path)
|
2020-09-01 11:17:43 +02:00
|
|
|
(funcall bookmark-handle-directory path))
|
2019-10-09 13:13:56 +02:00
|
|
|
((f-file? path)
|
2020-09-01 11:17:43 +02:00
|
|
|
(funcall bookmark-handle-file path)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2021-11-08 19:36:45 +01:00
|
|
|
(defun bookmark-install-kbd (b)
|
|
|
|
"Define two functions to explore B and assign them to keybindings."
|
|
|
|
(eval `(defun ,(intern (format "bookmark-visit-%s" (bookmark-label b))) ()
|
|
|
|
(interactive)
|
|
|
|
(find-file ,(bookmark-path b))))
|
|
|
|
(eval `(defun ,(intern (format "bookmark-browse-%s" (bookmark-label b))) ()
|
|
|
|
(interactive)
|
|
|
|
(bookmark-open ,b)))
|
|
|
|
(general-define-key
|
|
|
|
:prefix "<SPC>"
|
|
|
|
:states '(motion)
|
|
|
|
(format "J%s" (bookmark-kbd b)) `,(intern (format "bookmark-visit-%s" (bookmark-label b)))
|
|
|
|
(format "j%s" (bookmark-kbd b)) `,(intern (format "bookmark-browse-%s" (bookmark-label b)))))
|
2020-09-02 15:09:15 +02:00
|
|
|
|
|
|
|
(defun bookmark-install-kbds ()
|
|
|
|
"Install the keybindings defined herein."
|
2020-09-01 11:17:43 +02:00
|
|
|
(->> bookmark-whitelist
|
2021-11-08 19:36:45 +01:00
|
|
|
(-map #'bookmark-install-kbd)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'bookmark)
|
|
|
|
;;; bookmark.el ends here
|