diff options
| author | Dmitry Gutov | 2020-05-12 03:22:30 +0300 |
|---|---|---|
| committer | Dmitry Gutov | 2020-05-12 03:22:30 +0300 |
| commit | d98038e3cd68baa69489551315eaf70663bd1812 (patch) | |
| tree | 4a06d0336796b2fadb44a3ef45c02ac3c4e88d65 | |
| parent | e1184e357d9d81df0d01fd8416a399b20bf53081 (diff) | |
| download | emacs-d98038e3cd68baa69489551315eaf70663bd1812.tar.gz emacs-d98038e3cd68baa69489551315eaf70663bd1812.zip | |
Simplify a little, and avoid duplicate commands
* lisp/progmodes/project.el:
(project--transient-p) Remove, not needed.
(project-current): Move project-find based on the directory here.
(project--remove-from-project-list): Only write if the list changed.
(project-find-project): Rename to project-prompt-project-dir.
Simply return the directory selected by the user.
(project-switch-project-find-file): Remove.
(project-switch-project-dired): Rename to project-dired and make
it follow the convention of existing projec tcommands.
(project-switch-project-eshell): Ditto.
(project-switch-project): Instead of passing the project instance
to the command, just bind default-directory.
| -rw-r--r-- | lisp/progmodes/project.el | 84 |
1 files changed, 34 insertions, 50 deletions
diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el index 7d260a36036..08eb774b852 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el | |||
| @@ -96,10 +96,6 @@ Each functions on this hook is called in turn with one | |||
| 96 | argument (the directory) and should return either nil to mean | 96 | argument (the directory) and should return either nil to mean |
| 97 | that it is not applicable, or a project instance.") | 97 | that it is not applicable, or a project instance.") |
| 98 | 98 | ||
| 99 | (defun project--transient-p (pr) | ||
| 100 | "Return non-nil if PR is a transient project." | ||
| 101 | (eq (car pr) 'transient)) | ||
| 102 | |||
| 103 | ;;;###autoload | 99 | ;;;###autoload |
| 104 | (defun project-current (&optional maybe-prompt dir) | 100 | (defun project-current (&optional maybe-prompt dir) |
| 105 | "Return the project instance in DIR or `default-directory'. | 101 | "Return the project instance in DIR or `default-directory'. |
| @@ -110,9 +106,12 @@ the user for a different project to look in." | |||
| 110 | (cond | 106 | (cond |
| 111 | (pr) | 107 | (pr) |
| 112 | (maybe-prompt | 108 | (maybe-prompt |
| 113 | (setq pr (project-find-project)))) | 109 | (setq dir (project-prompt-project-dir) |
| 114 | (when (and pr (not (project--transient-p pr))) | 110 | pr (project--find-in-directory dir)))) |
| 115 | (project--add-to-project-list-front pr)) | 111 | (if pr |
| 112 | (project--add-to-project-list-front pr) | ||
| 113 | (project--remove-from-project-list dir) | ||
| 114 | (setq pr (cons 'transient dir))) | ||
| 116 | pr)) | 115 | pr)) |
| 117 | 116 | ||
| 118 | (defun project--find-in-directory (dir) | 117 | (defun project--find-in-directory (dir) |
| @@ -682,33 +681,28 @@ Return PR." | |||
| 682 | pr) | 681 | pr) |
| 683 | 682 | ||
| 684 | (defun project--remove-from-project-list (pr-dir) | 683 | (defun project--remove-from-project-list (pr-dir) |
| 685 | "Remove directory PR-DIR from the project list and save it." | 684 | "Remove directory PR-DIR from the project list. |
| 685 | If the directory was in the list before the removal, save the | ||
| 686 | result to disk." | ||
| 686 | (project--ensure-read-project-list) | 687 | (project--ensure-read-project-list) |
| 687 | (setq project--list (delete (list pr-dir) project--list)) | 688 | ;; XXX: This hardcodes that the number of roots = 1. |
| 688 | (project--write-project-list)) | 689 | ;; It's fine, though. |
| 689 | 690 | (when (member (list pr-dir) project--list) | |
| 690 | (defun project-find-project () | 691 | (setq project--list (delete (list pr-dir) project--list)) |
| 691 | "Prompt the user for a project and return it. | 692 | (message "Project `%s' not found; removed from list" pr-dir) |
| 693 | (project--write-project-list))) | ||
| 694 | |||
| 695 | (defun project-prompt-project-dir () | ||
| 696 | "Prompt the user for a directory from known project roots. | ||
| 692 | The project is chosen among projects known from the project list. | 697 | The project is chosen among projects known from the project list. |
| 693 | It's also possible to enter an arbitrary directory, in which case | 698 | It's also possible to enter an arbitrary directory." |
| 694 | a project for that directory is returned (possibly a transient | ||
| 695 | one). Return nil if no project or directory was chosen." | ||
| 696 | (project--ensure-read-project-list) | 699 | (project--ensure-read-project-list) |
| 697 | (let* ((dir-choice "... (choose a dir)") | 700 | (let* ((dir-choice "... (choose a dir)") |
| 698 | (choices (append project--list `(,dir-choice))) | 701 | (choices (append project--list `(,dir-choice))) |
| 699 | (pr-dir (completing-read "Project: " choices))) | 702 | (pr-dir (completing-read "Project: " choices))) |
| 700 | (if (equal pr-dir dir-choice) | 703 | (if (equal pr-dir dir-choice) |
| 701 | (let ((dir (read-directory-name | 704 | (read-directory-name "Choose directory: " default-directory nil t) |
| 702 | "Choose directory: " default-directory nil t))) | 705 | pr-dir))) |
| 703 | (if-let (pr (project--find-in-directory dir)) | ||
| 704 | (project--add-to-project-list-front pr) | ||
| 705 | (message "Using `%s' as a transient project root" dir) | ||
| 706 | (cons 'transient dir))) | ||
| 707 | (if-let (pr (project--find-in-directory pr-dir)) | ||
| 708 | (project--add-to-project-list-front pr) | ||
| 709 | (project--remove-from-project-list pr-dir) | ||
| 710 | (message "Project `%s' not found; removed from list" pr-dir) | ||
| 711 | nil)))) | ||
| 712 | 706 | ||
| 713 | 707 | ||
| 714 | ;;; Project switching | 708 | ;;; Project switching |
| @@ -719,28 +713,17 @@ Used by `project-switch-project' to construct a dispatch menu of | |||
| 719 | commands available for \"switching\" to another project.") | 713 | commands available for \"switching\" to another project.") |
| 720 | 714 | ||
| 721 | ;;;###autoload | 715 | ;;;###autoload |
| 722 | (defun project-switch-project-find-file (&optional pr) | 716 | (defun project-dired () |
| 723 | "\"Switch\" to project PR by finding a file in it. | 717 | "Open Dired in the current project." |
| 724 | If PR is nil, prompt for a project." | ||
| 725 | (interactive) | ||
| 726 | (setq pr (or pr (project-find-project))) | ||
| 727 | (let ((dirs (project-roots pr))) | ||
| 728 | (project-find-file-in nil dirs pr))) | ||
| 729 | |||
| 730 | ;;;###autoload | ||
| 731 | (defun project-switch-project-dired (&optional pr) | ||
| 732 | "\"Switch\" to project PR by visiting its root with Dired. | ||
| 733 | If PR is nil, prompt for a project." | ||
| 734 | (interactive) | 718 | (interactive) |
| 735 | (let ((dirs (project-roots (or pr (project-find-project))))) | 719 | (let ((dirs (project-roots (project-current t)))) |
| 736 | (dired (car dirs)))) | 720 | (dired (car dirs)))) |
| 737 | 721 | ||
| 738 | ;;;###autoload | 722 | ;;;###autoload |
| 739 | (defun project-switch-project-eshell (&optional pr) | 723 | (defun project-eshell () |
| 740 | "\"Switch\" to project PR by launching Eshell in its root. | 724 | "Open Eshell in the current project." |
| 741 | If PR is nil, prompt for a project." | ||
| 742 | (interactive) | 725 | (interactive) |
| 743 | (let* ((dirs (project-roots (or pr (project-find-project)))) | 726 | (let* ((dirs (project-roots (project-current t))) |
| 744 | (default-directory (car dirs))) | 727 | (default-directory (car dirs))) |
| 745 | (eshell t))) | 728 | (eshell t))) |
| 746 | 729 | ||
| @@ -753,13 +736,13 @@ LABEL is used to distinguish the function in the dispatch menu." | |||
| 753 | (define-key project-switch-keymap key symbol)) | 736 | (define-key project-switch-keymap key symbol)) |
| 754 | 737 | ||
| 755 | (project-add-switch-command | 738 | (project-add-switch-command |
| 756 | 'project-switch-project-find-file "f" "Find file") | 739 | 'project-find-file "f" "Find file") |
| 757 | 740 | ||
| 758 | (project-add-switch-command | 741 | (project-add-switch-command |
| 759 | 'project-switch-project-dired "d" "Dired") | 742 | 'project-dired "d" "Dired") |
| 760 | 743 | ||
| 761 | (project-add-switch-command | 744 | (project-add-switch-command |
| 762 | 'project-switch-project-eshell "e" "Eshell") | 745 | 'project-eshell "e" "Eshell") |
| 763 | 746 | ||
| 764 | (defun project--keymap-prompt () | 747 | (defun project--keymap-prompt () |
| 765 | "Return a prompt for the project swithing dispatch menu." | 748 | "Return a prompt for the project swithing dispatch menu." |
| @@ -778,15 +761,16 @@ LABEL is used to distinguish the function in the dispatch menu." | |||
| 778 | The available commands are picked from `project-switch-keymap' | 761 | The available commands are picked from `project-switch-keymap' |
| 779 | and presented in a dispatch menu." | 762 | and presented in a dispatch menu." |
| 780 | (interactive) | 763 | (interactive) |
| 781 | (let ((pr (project-find-project)) | 764 | (let* ((dir (project-prompt-project-dir)) |
| 782 | (choice nil)) | 765 | (choice nil)) |
| 783 | (while (not (and choice | 766 | (while (not (and choice |
| 784 | (or (equal choice (kbd "C-g")) | 767 | (or (equal choice (kbd "C-g")) |
| 785 | (lookup-key project-switch-keymap choice)))) | 768 | (lookup-key project-switch-keymap choice)))) |
| 786 | (setq choice (read-key-sequence (project--keymap-prompt)))) | 769 | (setq choice (read-key-sequence (project--keymap-prompt)))) |
| 787 | (if (equal choice (kbd "C-g")) | 770 | (if (equal choice (kbd "C-g")) |
| 788 | (message "Quit") | 771 | (message "Quit") |
| 789 | (funcall (lookup-key project-switch-keymap choice) pr)))) | 772 | (let ((default-directory dir)) |
| 773 | (funcall (lookup-key project-switch-keymap choice)))))) | ||
| 790 | 774 | ||
| 791 | (provide 'project) | 775 | (provide 'project) |
| 792 | ;;; project.el ends here | 776 | ;;; project.el ends here |