Skip to content

Commit

Permalink
[Fix #896] Add previous/next project buffer commands (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid authored and bbatsov committed Oct 3, 2018
1 parent 702a1e3 commit b94b05b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Add [crystal](https://crystal-lang.org) project type.
* [#850](https://github.com/bbatsov/projectile/issues/850): Make it possible to prompt for a project, when you're not in a project, instead of raising an error. (see `projectile-require-project-root`).
* **(Breaking)** [#1147](https://github.com/bbatsov/projectile/issues/1147): Introduce a faster indexing method - `turbo-alien` (it's the default now).
* [#896](https://github.com/bbatsov/projectile/issues/896) Add commands `projectile-previous-project-buffer ` and
`projectile-next-project-buffer ` to switch to other buffer in the project.

### Changes

Expand Down
2 changes: 2 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Keybinding | Description
<kbd>s-p 4 D</kbd> | Opens the root of the project in `dired` in another window.
<kbd>s-p 5 D</kbd> | Opens the root of the project in `dired` in another frame.
<kbd>s-p e</kbd> | Shows a list of recently visited project files.
<kbd>s-p left</kbd> | Switch to the previous project buffer.
<kbd>s-p right</kbd> | Switch to the next project buffer.
<kbd>s-p E</kbd> | Opens the root `dir-locals-file` of the project.
<kbd>s-p s s</kbd> | Runs `ag` on the project. Requires the presence of `ag.el`.
<kbd>s-p !</kbd> | Runs `shell-command` in the root directory of the project.
Expand Down
37 changes: 37 additions & 0 deletions projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,7 @@ If PROJECT is not specified the command acts on the current project."
(with-current-buffer buffer
(and (not (string-prefix-p " " (buffer-name buffer)))
(not (projectile-ignored-buffer-p buffer))
default-directory
(string-equal (file-remote-p default-directory)
(file-remote-p project-root))
(not (string-match-p "^http\\(s\\)?://" default-directory))
Expand Down Expand Up @@ -4025,6 +4026,8 @@ thing shown in the mode line otherwise."
(define-key map (kbd "x t") #'projectile-run-term)
(define-key map (kbd "x s") #'projectile-run-shell)
(define-key map (kbd "z") #'projectile-cache-current-file)
(define-key map (kbd "<left>") #'projectile-previous-project-buffer)
(define-key map (kbd "<right>") #'projectile-next-project-buffer)
(define-key map (kbd "ESC") #'projectile-project-buffers-other-buffer)
map)
"Keymap for Projectile commands after `projectile-keymap-prefix'.")
Expand All @@ -4048,6 +4051,8 @@ thing shown in the mode line otherwise."
["Kill project buffers" projectile-kill-buffers]
["Save project buffers" projectile-save-buffers]
["Recent files" projectile-recentf]
["Previous buffer" projectile-previous-project-buffer]
["Next buffer" projectile-next-project-buffer]
"--"
["Toggle project wide read-only" projectile-toggle-project-read-only]
["Edit .dir-locals.el" projectile-edit-dir-locals]
Expand Down Expand Up @@ -4144,6 +4149,38 @@ Otherwise behave as if called interactively.
;;;###autoload
(define-obsolete-function-alias 'projectile-global-mode 'projectile-mode "1.0")

(defun projectile--repeat-until-project-buffer (orig-fun &rest args)
"Repeat ORIG-FUN with ARGS until the current buffer is a project buffer."
(if (projectile-project-root)
(let* ((other-project-buffers (make-hash-table :test 'eq))
(projectile-project-buffers (projectile-project-buffers))
(max-iterations (length (buffer-list)))
(counter 0))
(dolist (buffer projectile-project-buffers)
(unless (eq buffer (current-buffer))
(puthash buffer t other-project-buffers)))
(when (cdr-safe projectile-project-buffers)
(while (and (< counter max-iterations)
(not (gethash (current-buffer) other-project-buffers)))
(apply orig-fun args)
(incf counter))))
(apply orig-fun args)))

(defun projectile-next-project-buffer ()
"In selected window switch to the next project buffer.
If the current buffer does not belong to a project, call `next-buffer'."
(interactive)
(projectile--repeat-until-project-buffer #'next-buffer))

(defun projectile-previous-project-buffer ()
"In selected window switch to the previous project buffer.
If the current buffer does not belong to a project, call `previous-buffer'."
(interactive)
(projectile--repeat-until-project-buffer #'previous-buffer))


(provide 'projectile)

;;; projectile.el ends here

0 comments on commit b94b05b

Please sign in to comment.