From 9e30a9cd87c034156fadef62a53aba81317a9db5 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 7 Aug 2013 22:05:29 +0200 Subject: [PATCH] Signal error and specify name - desktop-registry.el (desktop-registry-add-directory): Add an optional NAME parameter which will be asked for (in an interactive context) when the universal argument is used. Print a message if the specified directory has already been registered, signal an error when the (given or generated) name has already been used and only add the directory if neither of these conditions occur. (desktop-registry-add-current-desktop): Add optional NAME parameter which will be asked for (in an interactive context) when the universal argument is used and pass it on to `desktop-registry-add-directory'. --- NEWS | 7 +++++++ README.org | 18 +++++++++++------- desktop-registry.el | 34 ++++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/NEWS b/NEWS index d8e1851..a38c431 100644 --- a/NEWS +++ b/NEWS @@ -12,3 +12,10 @@ - Add =desktop-registry-rename-desktop= command to rename a desktop. - Specify prompts for each action that wants a desktop selected. + + - Allow specifying of desktop name by using the universal argument. + + - Signal an error when the name for a desktop (either specified or + generated) already exists in the registry and show a message when + the specified directory has already been registered. Only add the + directory if neither of these conditions occur. diff --git a/README.org b/README.org index 545cf7b..be611a4 100644 --- a/README.org +++ b/README.org @@ -14,13 +14,6 @@ to fix that. * Usage - You can use =desktop-registry-prepend-directory= to add a new - directory to the registry, or - =desktop-registry-prepend-current-desktop= to add the currently loaded - desktop to the registry. If you enable - =desktop-registry-auto-register= it will automatically add new desktop - files to the registry when you use =desktop-save=. - To switch between desktops you can use =desktop-registry-change-desktop=, this will prompt (with completion) for the directory you would like to load. @@ -32,6 +25,17 @@ to fix that. the name you can use =desktop-registry-rename-desktop= to give it a new name. +** Adding new desktops + + You can use =desktop-registry-add-directory= to add a new directory + to the registry, or =desktop-registry-add-current-desktop= to add the + currently loaded desktop to the registry. With either of these + function you can use the universal argument (=C-u=) to specify a + name for the desktop as well. + + If you enable =desktop-registry-auto-register= it will automatically + add new desktop files to the registry when you use =desktop-save=. + ** With org-capture-templates The =desktop-registry-current-desktop= can be used to insert the diff --git a/desktop-registry.el b/desktop-registry.el index 5206b96..ba35d16 100644 --- a/desktop-registry.el +++ b/desktop-registry.el @@ -60,23 +60,33 @@ Returns DEFAULT when `desktop-dirname' is nil." default)) ;;;###autoload -(defun desktop-registry-add-directory (dir) - "Add DIR to the desktop registry." - (interactive "DDirectory: ") +(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: ")))) (let* ((clean-dir (desktop-registry--canonicalize-dir dir)) - (label (file-name-base clean-dir))) - (unless (assoc label desktop-registry-registry) - (customize-save-variable - 'desktop-registry-registry - (cons (cons label clean-dir) desktop-registry-registry))))) + (label (or name (file-name-base clean-dir)))) + (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)))))) ;;;###autoload -(defun desktop-registry-add-current-desktop () - "Add the currently opened desktop file to `desktop-registry-registry'." - (interactive) +(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: ")))) (unless desktop-dirname (error "No desktop loaded")) - (desktop-registry-add-directory desktop-dirname)) + (desktop-registry-add-directory desktop-dirname name)) (defun desktop-registry--completing-read (&optional prompt default-current)