2013-08-07 15:59:42 +02:00
|
|
|
;;; desktop-registry.el --- Keep a central registry of desktop files -*- lexical-binding: t -*-
|
2013-06-04 01:51:09 +02:00
|
|
|
|
|
|
|
;; Copyright (C) 2013 Tom Willemse
|
|
|
|
|
|
|
|
;; Author: Tom Willemse <tom@ryuslash.org>
|
|
|
|
;; Keywords: convenience
|
2013-08-07 22:27:50 +02:00
|
|
|
;; Version: 1.1.0
|
2013-06-04 01:51:09 +02:00
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This module provides functions and a global minor mode that lets
|
|
|
|
;; you track a central registry of desktop files. This is useful when
|
|
|
|
;; you use desktop files as project files and want to be able to
|
|
|
|
;; switch between them quickly.
|
|
|
|
|
|
|
|
(require 'desktop)
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(defgroup desktop-registry nil
|
|
|
|
"Customization group for desktop-registry."
|
|
|
|
:group 'desktop
|
|
|
|
:prefix 'desktop-registry)
|
|
|
|
|
|
|
|
(defcustom desktop-registry-registry nil
|
|
|
|
"The registry of desktop files."
|
|
|
|
:group 'desktop-registry
|
|
|
|
:type '(repeat (cons string directory)))
|
|
|
|
|
2014-01-19 14:40:14 +01:00
|
|
|
(defcustom desktop-registry-list-switch-buffer-function
|
|
|
|
#'switch-to-buffer-other-window
|
|
|
|
"The function to use to switch to the desktop list buffer."
|
|
|
|
:group 'desktop-registry
|
|
|
|
:type 'function)
|
|
|
|
|
2013-06-04 01:51:09 +02:00
|
|
|
(defvar desktop-registry--history nil
|
|
|
|
"History variable for `desktop-registry'.")
|
|
|
|
|
2014-01-19 14:50:10 +01:00
|
|
|
(defvar desktop-registry-list-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(set-keymap-parent map tabulated-list-mode-map)
|
|
|
|
(define-key map "o" #'desktop-registry-change-desktop)
|
|
|
|
(define-key map "R" #'desktop-registry-rename-desktop)
|
|
|
|
(define-key map "d" #'desktop-registry-remove-desktop)
|
|
|
|
(define-key map "a" #'desktop-registry-add-directory)
|
|
|
|
(define-key map "A" #'desktop-registry-add-current-desktop)
|
|
|
|
map))
|
|
|
|
|
2013-08-06 17:04:18 +02:00
|
|
|
(defun desktop-registry--canonicalize-dir (dir)
|
|
|
|
"Canonicalize DIR for use."
|
|
|
|
(directory-file-name (expand-file-name dir)))
|
|
|
|
|
2014-01-19 14:50:10 +01:00
|
|
|
(defun desktop-registry--desktop-in-row ()
|
|
|
|
"If `desktop-registry-list-mode' is active, return the current rowid."
|
|
|
|
(and (eql major-mode 'desktop-registry-list-mode)
|
|
|
|
(tabulated-list-get-id)))
|
|
|
|
|
2013-08-06 17:04:18 +02:00
|
|
|
;;;###autoload
|
2013-08-07 15:55:48 +02:00
|
|
|
(defun desktop-registry-current-desktop (&optional default)
|
2013-08-06 17:04:18 +02:00
|
|
|
"Get the name of the currently loaded desktop.
|
|
|
|
|
2013-08-07 15:55:48 +02:00
|
|
|
Returns DEFAULT when `desktop-dirname' is nil."
|
2013-08-06 17:04:18 +02:00
|
|
|
(if desktop-dirname
|
|
|
|
(let ((canonical
|
|
|
|
(desktop-registry--canonicalize-dir desktop-dirname)))
|
|
|
|
(car (cl-find-if (lambda (d) (equal (cdr d) canonical))
|
|
|
|
desktop-registry-registry)))
|
2013-08-07 15:55:48 +02:00
|
|
|
default))
|
2013-08-06 17:04:18 +02:00
|
|
|
|
2013-06-04 01:51:09 +02:00
|
|
|
;;;###autoload
|
2013-08-07 22:05:29 +02:00
|
|
|
(defun desktop-registry-add-directory (dir &optional name)
|
|
|
|
"Add DIR to the desktop registry, possibly using NAME."
|
|
|
|
(interactive (list (read-directory-name "Directory: ")
|
|
|
|
(if (equal current-prefix-arg '(4))
|
|
|
|
(read-string "Name: "))))
|
2013-08-06 17:04:18 +02:00
|
|
|
(let* ((clean-dir (desktop-registry--canonicalize-dir dir))
|
2014-01-19 14:58:07 +01:00
|
|
|
(label (or name (file-name-nondirectory clean-dir))))
|
2013-08-07 22:05:29 +02:00
|
|
|
(cond
|
|
|
|
((cl-find clean-dir desktop-registry-registry
|
|
|
|
:key 'cdr :test 'equal)
|
|
|
|
(message "Directory %s already registered" clean-dir))
|
|
|
|
((cl-find label desktop-registry-registry :key 'car :test 'equal)
|
|
|
|
(error "Name %s already used" label))
|
|
|
|
(t (customize-save-variable
|
|
|
|
'desktop-registry-registry
|
|
|
|
(cons (cons label clean-dir) desktop-registry-registry))))))
|
2013-06-04 01:51:09 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2013-08-07 22:05:29 +02:00
|
|
|
(defun desktop-registry-add-current-desktop (&optional name)
|
|
|
|
"Add the currently opened desktop file to `desktop-registry-registry'.
|
|
|
|
|
|
|
|
If NAME is specified use that as the name for the registry entry."
|
|
|
|
(interactive (list (if (equal current-prefix-arg '(4))
|
|
|
|
(read-string "Name: "))))
|
2013-06-04 01:51:09 +02:00
|
|
|
(unless desktop-dirname
|
|
|
|
(error "No desktop loaded"))
|
2013-08-07 22:05:29 +02:00
|
|
|
(desktop-registry-add-directory desktop-dirname name))
|
2013-06-04 01:51:09 +02:00
|
|
|
|
2013-08-07 15:55:48 +02:00
|
|
|
(defun desktop-registry--completing-read (&optional prompt
|
|
|
|
default-current)
|
2013-08-07 15:58:47 +02:00
|
|
|
"Ask the user to pick a desktop directory.
|
|
|
|
|
|
|
|
PROMPT specifies the prompt to use when asking, which defaults to
|
|
|
|
\"Desktop: \". DEFAULT-CURRENT specifies whether to use the
|
|
|
|
current desktop as default value."
|
2013-08-07 15:55:48 +02:00
|
|
|
(let ((prompt (or prompt "Desktop: "))
|
|
|
|
(default (and default-current
|
|
|
|
(desktop-registry-current-desktop))))
|
2013-08-07 15:19:06 +02:00
|
|
|
(completing-read prompt desktop-registry-registry nil nil nil
|
2013-08-07 15:55:48 +02:00
|
|
|
'desktop-registry--history default)))
|
2013-08-06 15:53:20 +02:00
|
|
|
|
2014-01-19 14:50:10 +01:00
|
|
|
(defun desktop-registry--get-desktop-name (&optional prompt
|
|
|
|
default-current)
|
|
|
|
"Get the name of a desktop.
|
|
|
|
|
|
|
|
This is done by either looking at the desktop name at point, in
|
|
|
|
case `desktop-registry-list-mode' is active, or asks the user to
|
|
|
|
provide a name with completion. The parameters PROMPT and
|
|
|
|
DEFAULT-CURRENT are passed directly to
|
|
|
|
`desktop-registry--completing-read' when no desktop is found at
|
|
|
|
point."
|
|
|
|
(or (desktop-registry--desktop-in-row)
|
|
|
|
(desktop-registry--completing-read prompt default-current)))
|
|
|
|
|
2013-08-06 15:53:20 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun desktop-registry-remove-desktop (desktop)
|
|
|
|
"Remove DESKTOP from the desktop registry."
|
2014-01-19 14:50:10 +01:00
|
|
|
(interactive (list (desktop-registry--get-desktop-name "Remove: " t)))
|
2013-08-06 15:53:20 +02:00
|
|
|
(let ((spec (assoc desktop desktop-registry-registry)))
|
|
|
|
(if spec
|
|
|
|
(customize-save-variable
|
|
|
|
'desktop-registry-registry
|
|
|
|
(delete spec desktop-registry-registry))
|
|
|
|
(error "Unknown desktop: %s" desktop))))
|
|
|
|
|
2013-08-07 15:10:08 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun desktop-registry-rename-desktop (old new)
|
2013-08-07 15:58:47 +02:00
|
|
|
"Rename desktop OLD to NEW."
|
2014-01-19 14:50:10 +01:00
|
|
|
(interactive (list (desktop-registry--get-desktop-name "Rename: " t)
|
2013-08-07 15:10:08 +02:00
|
|
|
(read-string "to: ")))
|
|
|
|
(let ((spec (assoc old desktop-registry-registry)))
|
|
|
|
(if (not spec)
|
|
|
|
(error "Unknown desktop: %s" old)
|
|
|
|
(setf (car spec) new)
|
|
|
|
(customize-save-variable 'desktop-registry-registry
|
|
|
|
desktop-registry-registry))))
|
|
|
|
|
2013-06-04 01:51:09 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun desktop-registry-change-desktop (name)
|
|
|
|
"Change to the desktop named NAME."
|
2014-01-19 14:50:10 +01:00
|
|
|
(interactive (list (desktop-registry--get-desktop-name "Switch to: ")))
|
2013-06-04 01:51:09 +02:00
|
|
|
(desktop-change-dir (cdr (assoc name desktop-registry-registry))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode desktop-registry-auto-register
|
|
|
|
"Automatically add saved desktops to the registry."
|
|
|
|
:global t
|
|
|
|
(if desktop-registry-auto-register
|
|
|
|
(add-hook 'desktop-save-hook
|
|
|
|
'desktop-registry-add-current-desktop)
|
|
|
|
(remove-hook 'desktop-save-hook
|
|
|
|
'desktop-registry-add-current-desktop)))
|
|
|
|
|
2014-01-19 14:40:14 +01:00
|
|
|
(defun desktop-registry--prepare-row (data)
|
|
|
|
"Format a row of DATA for `tabulated-list-entries'."
|
|
|
|
(let* ((name (car data))
|
|
|
|
(dir (cdr data))
|
|
|
|
(existsp (and (file-exists-p dir)
|
|
|
|
(file-directory-p dir))))
|
|
|
|
(list name (vector name (if existsp "yes" "no") dir))))
|
|
|
|
|
|
|
|
(defun desktop-registry--refresh-list ()
|
|
|
|
"Fill `tabulated-list-entries' with registered desktops."
|
|
|
|
(setq tabulated-list-entries
|
|
|
|
(mapcar #'desktop-registry--prepare-row
|
|
|
|
desktop-registry-registry)))
|
|
|
|
|
|
|
|
(define-derived-mode desktop-registry-list-mode tabulated-list-mode
|
|
|
|
"Desktop Registry"
|
|
|
|
"Major mode for listing registered desktops.
|
|
|
|
|
|
|
|
\\<desktop-registry-list-mode-map>
|
|
|
|
\\{desktop-registry-list-mode-map}"
|
|
|
|
(setq tabulated-list-format [("Label" 30 t)
|
|
|
|
("Exists" 6 nil)
|
|
|
|
("Location" 0 t)]
|
|
|
|
tabulated-list-sort-key '("Label"))
|
|
|
|
(add-hook 'tabulated-list-revert-hook #'desktop-registry--refresh-list)
|
|
|
|
(tabulated-list-init-header))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun desktop-registry-list-desktops ()
|
|
|
|
"Display a list of registered desktops."
|
|
|
|
(interactive)
|
|
|
|
(let ((buffer (get-buffer-create "*Desktop Registry*")))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(desktop-registry-list-mode)
|
|
|
|
(desktop-registry--refresh-list)
|
|
|
|
(tabulated-list-print))
|
|
|
|
(funcall desktop-registry-list-switch-buffer-function buffer))
|
|
|
|
nil)
|
|
|
|
|
2013-06-04 01:51:09 +02:00
|
|
|
(provide 'desktop-registry)
|
|
|
|
;;; desktop-registry.el ends here
|