aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Gutov2020-12-13 22:50:46 +0200
committerDmitry Gutov2020-12-13 22:50:55 +0200
commit51698f77dd6356049fcacdb01ebe80cfe4c67272 (patch)
treeb8839f1c39ec926a5732096b915f6607e609374a
parentfe50a8b9ba79b4ac14a3a352d8bf84eaee4f2122 (diff)
downloademacs-51698f77dd6356049fcacdb01ebe80cfe4c67272.tar.gz
emacs-51698f77dd6356049fcacdb01ebe80cfe4c67272.zip
Remove the duplication from project-switch-commands's config
Based on an older patch by Philip K (https://debbugs.gnu.org/41890#127). * lisp/progmodes/project.el: (project-switch-commands): Change to 'defcustom', alter the value format, add :type. (project-switch-use-entire-map): New option. (project--keymap-prompt, project-switch-project): Update accordingly, while keeping compatibility with user-defined values in the previous format (for some transition period). Co-authored-by: Philip K. <philipk@posteo.net>
-rw-r--r--lisp/progmodes/project.el84
1 files changed, 65 insertions, 19 deletions
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 0ed5f1f907c..d4c0e46c851 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1250,27 +1250,55 @@ It's also possible to enter an arbitrary directory not in the list."
1250;;; Project switching 1250;;; Project switching
1251 1251
1252;;;###autoload 1252;;;###autoload
1253(defvar project-switch-commands 1253(defcustom project-switch-commands
1254 '((?f "Find file" project-find-file) 1254 '((project-find-file "Find file")
1255 (?g "Find regexp" project-find-regexp) 1255 (project-find-regexp "Find regexp")
1256 (?d "Dired" project-dired) 1256 (project-dired "Dired")
1257 (?v "VC-Dir" project-vc-dir) 1257 (project-vc-dir "VC-Dir")
1258 (?e "Eshell" project-eshell)) 1258 (project-eshell "Eshell"))
1259 "Alist mapping keys to project switching menu entries. 1259 "Alist mapping commands to descriptions.
1260Used by `project-switch-project' to construct a dispatch menu of 1260Used by `project-switch-project' to construct a dispatch menu of
1261commands available upon \"switching\" to another project. 1261commands available upon \"switching\" to another project.
1262 1262
1263Each element is of the form (KEY LABEL COMMAND), where COMMAND is the 1263Each element is of the form (COMMAND LABEL &optional KEY) where
1264command to run when KEY is pressed. LABEL is used to distinguish 1264COMMAND is the command to run when KEY is pressed. LABEL is used
1265the menu entries in the dispatch menu.") 1265to distinguish the menu entries in the dispatch menu. If KEY is
1266absent, COMMAND must be bound in `project-prefix-map', and the
1267key is looked up in that map."
1268 :version "28.1"
1269 :package-version '(project . "0.6.0")
1270 :type '(repeat
1271 (list
1272 (symbol :tag "Command")
1273 (string :tag "Label")
1274 (choice :tag "Key to press"
1275 (const :tag "Infer from the keymap" nil)
1276 (character :tag "Explicit key")))))
1277
1278(defcustom project-switch-use-entire-map nil
1279 "Make `project-switch-project' use entire `project-prefix-map'.
1280If nil, `project-switch-project' will only recognize commands
1281listed in `project-switch-commands' and signal an error when
1282others are invoked. Otherwise, all keys in `project-prefix-map'
1283are legal even if they aren't listed in the dispatch menu."
1284 :type 'bool
1285 :version "28.1")
1266 1286
1267(defun project--keymap-prompt () 1287(defun project--keymap-prompt ()
1268 "Return a prompt for the project switching dispatch menu." 1288 "Return a prompt for the project switching dispatch menu."
1269 (mapconcat 1289 (mapconcat
1270 (pcase-lambda (`(,key ,label)) 1290 (pcase-lambda (`(,cmd ,label ,key))
1271 (format "[%s] %s" 1291 (when (characterp cmd) ; Old format, apparently user-customized.
1272 (propertize (key-description `(,key)) 'face 'bold) 1292 (let ((tmp cmd))
1273 label)) 1293 ;; TODO: Add a deprecation warning, probably.
1294 (setq cmd key
1295 key tmp)))
1296 (let ((key (if key
1297 (vector key)
1298 (where-is-internal cmd project-prefix-map t))))
1299 (format "[%s] %s"
1300 (propertize (key-description key) 'face 'bold)
1301 label)))
1274 project-switch-commands 1302 project-switch-commands
1275 " ")) 1303 " "))
1276 1304
@@ -1283,13 +1311,31 @@ made from `project-switch-commands'.
1283When called in a program, it will use the project corresponding 1311When called in a program, it will use the project corresponding
1284to directory DIR." 1312to directory DIR."
1285 (interactive (list (project-prompt-project-dir))) 1313 (interactive (list (project-prompt-project-dir)))
1286 (let ((choice nil)) 1314 (let ((commands-menu
1287 (while (not choice) 1315 (mapcar
1288 (setq choice (assq (read-event (project--keymap-prompt)) 1316 (lambda (row)
1289 project-switch-commands))) 1317 (if (characterp (car row))
1318 ;; Deprecated format.
1319 ;; XXX: Add a warning about it?
1320 (reverse row)
1321 row))
1322 project-switch-commands))
1323 command)
1324 (while (not command)
1325 (let ((choice (read-event (project--keymap-prompt))))
1326 (when (setq command
1327 (or (car
1328 (seq-find (lambda (row) (equal choice (nth 2 row)))
1329 commands-menu))
1330 (lookup-key project-prefix-map (vector choice))))
1331 (unless (or project-switch-use-entire-map
1332 (assq command commands-menu))
1333 ;; TODO: Add some hint to the prompt, like "key not
1334 ;; recognized" or something.
1335 (setq command nil)))))
1290 (let ((default-directory dir) 1336 (let ((default-directory dir)
1291 (project-current-inhibit-prompt t)) 1337 (project-current-inhibit-prompt t))
1292 (call-interactively (nth 2 choice))))) 1338 (call-interactively command))))
1293 1339
1294(provide 'project) 1340(provide 'project)
1295;;; project.el ends here 1341;;; project.el ends here