Download
(require 'project-buffer-mode)
(defun project-buffer-mode-p-attach-project-buffer(project-buffer file-buffer)
"Attach PROJECT-BUFFER buffer to the buffer FILE-BUFFER."
(with-current-buffer file-buffer
(unless (local-variable-p 'project-buffer-mode-p-attached-project-buffer)
(make-local-variable 'project-buffer-mode-p-attached-project-buffer))
(setq project-buffer-mode-p-attached-project-buffer project-buffer)))
(defun project-buffer-mode-p-get-attached-project-buffer()
"Get the attached project-buffer."
(when (and (local-variable-p 'project-buffer-mode-p-attached-project-buffer)
(bufferp project-buffer-mode-p-attached-project-buffer))
project-buffer-mode-p-attached-project-buffer))
(defun project-buffer-mode-p-run-project-buffer-build-action()
"Kick the 'build action from the attached project-buffer."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(with-current-buffer buffer
(project-buffer-perform-build-action))))
(defun project-buffer-mode-p-run-project-buffer-clean-action()
"Kick the 'clean action from the attached project-buffer."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(with-current-buffer buffer
(project-buffer-perform-clean-action))))
(defun project-buffer-mode-p-run-project-buffer-run-action()
"Kick the 'run action from the attached project-buffer."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(with-current-buffer buffer
(project-buffer-perform-run-action))))
(defun project-buffer-mode-p-run-project-buffer-debug-action()
"Kick the 'debug action from the attached project-buffer."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(with-current-buffer buffer
(project-buffer-perform-debug-action))))
(defun project-buffer-mode-p-run-project-buffer-update-action()
"Kick the 'update action from the attached project-buffer."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(with-current-buffer buffer
(project-buffer-perform-update-action))))
(defun project-buffer-mode-p-go-to-attached-project-buffer()
"Go to the project-buffer attached to the current file."
(interactive)
(let ((buffer (project-buffer-mode-p-get-attached-project-buffer)))
(unless buffer (error "No project-buffer attached to this file"))
(switch-to-buffer buffer)))
(defun project-buffer-mode-p-register-project-to-file(project-buffer file-buffer)
"Register the PROJECT-BUFFER to the FILE-BUFFER.
This will allow to retrieve the buffer."
(project-buffer-mode-p-attach-project-buffer project-buffer file-buffer))
(defun project-buffer-mode-p-link-buffers-to-current-project(project-list content)
"Check the different opened file buffer to see if they belong to the project.
Note: technically it's possible to also limit the research to the
current project or to the projects in the list. I don't see why
someone would wanna do that!?"
(let ((project-buffer (current-buffer))
(buffers-assoc (remq nil (mapcar (lambda (cur-buf)
(let ((file (buffer-file-name cur-buf)))
(and file (cons (expand-file-name file) cur-buf))))
(buffer-list))))
(count 0))
(project-buffer-apply-to-each-file (lambda (project-file-name file-path project-name buffers)
(let ((assoc-data (assoc (expand-file-name file-path) buffers)))
(when assoc-data
(project-buffer-mode-p-attach-project-buffer project-buffer (cdr assoc-data))
(setq count (1+ count)))))
buffers-assoc)
(message "%i buffer%s attached to this project" count (if (> count 1) "s" ""))
))
(defun project-buffer-mode-p-setup(&optional no-key-bindings)
"Setup the hook and the global keuys if KEY-BINDINGS is set to t."
(add-hook 'project-buffer-post-find-file-hook 'project-buffer-mode-p-register-project-to-file)
(add-hook 'project-buffer-refresh-hook 'project-buffer-mode-p-link-buffers-to-current-project)
(unless no-key-bindings
(define-key global-map [(control x) (?p) (?s)] 'project-buffer-mode-p-go-to-attached-project-buffer)
(define-key global-map [(control x) (?p) (?B)] 'project-buffer-mode-p-run-project-buffer-build-action)
(define-key global-map [(control x) (?p) (?C)] 'project-buffer-mode-p-run-project-buffer-clean-action)
(define-key global-map [(control x) (?p) (?R)] 'project-buffer-mode-p-run-project-buffer-run-action)
(define-key global-map [(control x) (?p) (?D)] 'project-buffer-mode-p-run-project-buffer-debug-action)
(define-key global-map [(control x) (?p) (?U)] 'project-buffer-mode-p-run-project-buffer-update-action)
))
(provide 'project-buffer-mode+)