diff options
| author | Dmitry Gutov | 2020-05-12 03:22:30 +0300 |
|---|---|---|
| committer | Simen Heggestøyl | 2020-05-26 17:41:24 +0200 |
| commit | 9f88356b6770fb710c47e277dbdb4ba31b463d08 (patch) | |
| tree | d2990f3e3d2dbe2d6f576ea0a2a9f5170719ae17 | |
| parent | afb7602a24cdb38c02998cc1f3c538b31981b255 (diff) | |
| download | emacs-9f88356b6770fb710c47e277dbdb4ba31b463d08.tar.gz emacs-9f88356b6770fb710c47e277dbdb4ba31b463d08.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 e77416397bc..fd691be5599 100644 --- a/lisp/progmodes/project.el +++ b/lisp/progmodes/project.el | |||
| @@ -101,10 +101,6 @@ Each functions on this hook is called in turn with one | |||
| 101 | argument (the directory) and should return either nil to mean | 101 | argument (the directory) and should return either nil to mean |
| 102 | that it is not applicable, or a project instance.") | 102 | that it is not applicable, or a project instance.") |
| 103 | 103 | ||
| 104 | (defun project--transient-p (pr) | ||
| 105 | "Return non-nil if PR is a transient project." | ||
| 106 | (eq (car pr) 'transient)) | ||
| 107 | |||
| 108 | ;;;###autoload | 104 | ;;;###autoload |
| 109 | (defun project-current (&optional maybe-prompt dir) | 105 | (defun project-current (&optional maybe-prompt dir) |
| 110 | "Return the project instance in DIR or `default-directory'. | 106 | "Return the project instance in DIR or `default-directory'. |
| @@ -115,9 +111,12 @@ the user for a different project to look in." | |||
| 115 | (cond | 111 | (cond |
| 116 | (pr) | 112 | (pr) |
| 117 | (maybe-prompt | 113 | (maybe-prompt |
| 118 | (setq pr (project-find-project)))) | 114 | (setq dir (project-prompt-project-dir) |
| 119 | (when (and pr (not (project--transient-p pr))) | 115 | pr (project--find-in-directory dir)))) |
| 120 | (project--add-to-project-list-front pr)) | 116 | (if pr |
| 117 | (project--add-to-project-list-front pr) | ||
| 118 | (project--remove-from-project-list dir) | ||
| 119 | (setq pr (cons 'transient dir))) | ||
| 121 | pr)) | 120 | pr)) |
| 122 | 121 | ||
| 123 | (defun project--find-in-directory (dir) | 122 | (defun project--find-in-directory (dir) |
| @@ -747,33 +746,28 @@ Return PR." | |||
| 747 | pr) | 746 | pr) |
| 748 | 747 | ||
| 749 | (defun project--remove-from-project-list (pr-dir) | 748 | (defun project--remove-from-project-list (pr-dir) |
| 750 | "Remove directory PR-DIR from the project list and save it." | 749 | "Remove directory PR-DIR from the project list. |
| 750 | If the directory was in the list before the removal, save the | ||
| 751 | result to disk." | ||
| 751 | (project--ensure-read-project-list) | 752 | (project--ensure-read-project-list) |
| 752 | (setq project--list (delete (list pr-dir) project--list)) | 753 | ;; XXX: This hardcodes that the number of roots = 1. |
| 753 | (project--write-project-list)) | 754 | ;; It's fine, though. |
| 754 | 755 | (when (member (list pr-dir) project--list) | |
| 755 | (defun project-find-project () | 756 | (setq project--list (delete (list pr-dir) project--list)) |
| 756 | "Prompt the user for a project and return it. | 757 | (message "Project `%s' not found; removed from list" pr-dir) |
| 758 | (project--write-project-list))) | ||
| 759 | |||
| 760 | (defun project-prompt-project-dir () | ||
| 761 | "Prompt the user for a directory from known project roots. | ||
| 757 | The project is chosen among projects known from the project list. | 762 | The project is chosen among projects known from the project list. |
| 758 | It's also possible to enter an arbitrary directory, in which case | 763 | It's also possible to enter an arbitrary directory." |
| 759 | a project for that directory is returned (possibly a transient | ||
| 760 | one). Return nil if no project or directory was chosen." | ||
| 761 | (project--ensure-read-project-list) | 764 | (project--ensure-read-project-list) |
| 762 | (let* ((dir-choice "... (choose a dir)") | 765 | (let* ((dir-choice "... (choose a dir)") |
| 763 | (choices (append project--list `(,dir-choice))) | 766 | (choices (append project--list `(,dir-choice))) |
| 764 | (pr-dir (completing-read "Project: " choices))) | 767 | (pr-dir (completing-read "Project: " choices))) |
| 765 | (if (equal pr-dir dir-choice) | 768 | (if (equal pr-dir dir-choice) |
| 766 | (let ((dir (read-directory-name | 769 | (read-directory-name "Choose directory: " default-directory nil t) |
| 767 | "Choose directory: " default-directory nil t))) | 770 | pr-dir))) |
| 768 | (if-let (pr (project--find-in-directory dir)) | ||
| 769 | (project--add-to-project-list-front pr) | ||
| 770 | (message "Using `%s' as a transient project root" dir) | ||
| 771 | (cons 'transient dir))) | ||
| 772 | (if-let (pr (project--find-in-directory pr-dir)) | ||
| 773 | (project--add-to-project-list-front pr) | ||
| 774 | (project--remove-from-project-list pr-dir) | ||
| 775 | (message "Project `%s' not found; removed from list" pr-dir) | ||
| 776 | nil)))) | ||
| 777 | 771 | ||
| 778 | 772 | ||
| 779 | ;;; Project switching | 773 | ;;; Project switching |
| @@ -784,28 +778,17 @@ Used by `project-switch-project' to construct a dispatch menu of | |||
| 784 | commands available for \"switching\" to another project.") | 778 | commands available for \"switching\" to another project.") |
| 785 | 779 | ||
| 786 | ;;;###autoload | 780 | ;;;###autoload |
| 787 | (defun project-switch-project-find-file (&optional pr) | 781 | (defun project-dired () |
| 788 | "\"Switch\" to project PR by finding a file in it. | 782 | "Open Dired in the current project." |
| 789 | If PR is nil, prompt for a project." | ||
| 790 | (interactive) | ||
| 791 | (setq pr (or pr (project-find-project))) | ||
| 792 | (let ((dirs (project-roots pr))) | ||
| 793 | (project-find-file-in nil dirs pr))) | ||
| 794 | |||
| 795 | ;;;###autoload | ||
| 796 | (defun project-switch-project-dired (&optional pr) | ||
| 797 | "\"Switch\" to project PR by visiting its root with Dired. | ||
| 798 | If PR is nil, prompt for a project." | ||
| 799 | (interactive) | 783 | (interactive) |
| 800 | (let ((dirs (project-roots (or pr (project-find-project))))) | 784 | (let ((dirs (project-roots (project-current t)))) |
| 801 | (dired (car dirs)))) | 785 | (dired (car dirs)))) |
| 802 | 786 | ||
| 803 | ;;;###autoload | 787 | ;;;###autoload |
| 804 | (defun project-switch-project-eshell (&optional pr) | 788 | (defun project-eshell () |
| 805 | "\"Switch\" to project PR by launching Eshell in its root. | 789 | "Open Eshell in the current project." |
| 806 | If PR is nil, prompt for a project." | ||
| 807 | (interactive) | 790 | (interactive) |
| 808 | (let* ((dirs (project-roots (or pr (project-find-project)))) | 791 | (let* ((dirs (project-roots (project-current t))) |
| 809 | (default-directory (car dirs))) | 792 | (default-directory (car dirs))) |
| 810 | (eshell t))) | 793 | (eshell t))) |
| 811 | 794 | ||
| @@ -818,13 +801,13 @@ LABEL is used to distinguish the function in the dispatch menu." | |||
| 818 | (define-key project-switch-keymap key symbol)) | 801 | (define-key project-switch-keymap key symbol)) |
| 819 | 802 | ||
| 820 | (project-add-switch-command | 803 | (project-add-switch-command |
| 821 | 'project-switch-project-find-file "f" "Find file") | 804 | 'project-find-file "f" "Find file") |
| 822 | 805 | ||
| 823 | (project-add-switch-command | 806 | (project-add-switch-command |
| 824 | 'project-switch-project-dired "d" "Dired") | 807 | 'project-dired "d" "Dired") |
| 825 | 808 | ||
| 826 | (project-add-switch-command | 809 | (project-add-switch-command |
| 827 | 'project-switch-project-eshell "e" "Eshell") | 810 | 'project-eshell "e" "Eshell") |
| 828 | 811 | ||
| 829 | (defun project--keymap-prompt () | 812 | (defun project--keymap-prompt () |
| 830 | "Return a prompt for the project swithing dispatch menu." | 813 | "Return a prompt for the project swithing dispatch menu." |
| @@ -843,15 +826,16 @@ LABEL is used to distinguish the function in the dispatch menu." | |||
| 843 | The available commands are picked from `project-switch-keymap' | 826 | The available commands are picked from `project-switch-keymap' |
| 844 | and presented in a dispatch menu." | 827 | and presented in a dispatch menu." |
| 845 | (interactive) | 828 | (interactive) |
| 846 | (let ((pr (project-find-project)) | 829 | (let* ((dir (project-prompt-project-dir)) |
| 847 | (choice nil)) | 830 | (choice nil)) |
| 848 | (while (not (and choice | 831 | (while (not (and choice |
| 849 | (or (equal choice (kbd "C-g")) | 832 | (or (equal choice (kbd "C-g")) |
| 850 | (lookup-key project-switch-keymap choice)))) | 833 | (lookup-key project-switch-keymap choice)))) |
| 851 | (setq choice (read-key-sequence (project--keymap-prompt)))) | 834 | (setq choice (read-key-sequence (project--keymap-prompt)))) |
| 852 | (if (equal choice (kbd "C-g")) | 835 | (if (equal choice (kbd "C-g")) |
| 853 | (message "Quit") | 836 | (message "Quit") |
| 854 | (funcall (lookup-key project-switch-keymap choice) pr)))) | 837 | (let ((default-directory dir)) |
| 838 | (funcall (lookup-key project-switch-keymap choice)))))) | ||
| 855 | 839 | ||
| 856 | (provide 'project) | 840 | (provide 'project) |
| 857 | ;;; project.el ends here | 841 | ;;; project.el ends here |