This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+spacemacs/spacemacs-project/funcs.el
2020-09-23 21:25:01 +02:00

110 lines
4.2 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; funcs.el --- Spacemacs Project Management Layer packages File
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Codruț Constantin Gușoi <codrut.gusoi@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs--projectile-directory-path ()
"Retrieve the directory path relative to project root.
If the buffer is not visiting a file, use the `list-buffers-directory'
variable as a fallback to display the directory, useful in buffers like the
ones created by `magit' and `dired'.
Returns:
- A string containing the directory path in case of success.
- `nil' in case the current buffer does not have a directory."
(when-let (directory-name (if-let (file-name (buffer-file-name))
(file-name-directory file-name)
list-buffers-directory))
(file-relative-name
(file-truename directory-name)
(projectile-project-root))))
(defun spacemacs--projectile-file-path ()
"Retrieve the file path relative to project root.
Returns:
- A string containing the file path in case of success.
- `nil' in case the current buffer does not visit a file."
(when-let (file-name (buffer-file-name))
(file-relative-name (file-truename file-name) (projectile-project-root))))
(defun spacemacs--projectile-file-path-with-line ()
"Retrieve the file path relative to project root, including line number.
Returns:
- A string containing the file path in case of success.
- `nil' in case the current buffer does not visit a file."
(when-let (file-path (spacemacs--projectile-file-path))
(concat file-path ":" (number-to-string (line-number-at-pos)))))
(defun spacemacs--projectile-file-path-with-line-column ()
"Retrieve the file path relative to project root, including line and column number.
This function respects the value of the `column-number-indicator-zero-based'
variable.
Returns:
- A string containing the file path in case of success.
- `nil' in case the current buffer does not visit a file."
(when-let (file-path (spacemacs--projectile-file-path-with-line))
(concat
file-path
":"
(number-to-string (if (and
;; Emacs 26 introduced this variable.
;; Remove this check once 26 becomes the minimum version.
(boundp column-number-indicator-zero-based)
(not column-number-indicator-zero-based))
(1+ (current-column))
(current-column))))))
(defun spacemacs/projectile-copy-directory-path ()
"Copy and show the directory path relative to project root.
If the buffer is not visiting a file, use the `list-buffers-directory'
variable as a fallback to display the directory, useful in buffers like the
ones created by `magit' and `dired'."
(interactive)
(if-let (directory-path (spacemacs--projectile-directory-path))
(progn
(kill-new directory-path)
(message "%s" directory-path))
(message "WARNING: Current buffer does not have a directory!")))
(defun spacemacs/projectile-copy-file-path ()
"Copy and show the file path relative to project root."
(interactive)
(if-let (file-path (spacemacs--projectile-file-path))
(progn
(kill-new file-path)
(message "%s" file-path))
(message "WARNING: Current buffer is not visiting a file!")))
(defun spacemacs/projectile-copy-file-path-with-line ()
"Copy and show the file path relative to project root, including line number."
(interactive)
(if-let (file-path (spacemacs--projectile-file-path-with-line))
(progn
(kill-new file-path)
(message "%s" file-path))
(message "WARNING: Current buffer is not visiting a file!")))
(defun spacemacs/projectile-copy-file-path-with-line-column ()
"Copy and show the file path relative to project root, including line and column number.
This function respects the value of the `column-number-indicator-zero-based'
variable."
(interactive)
(if-let (file-path (spacemacs--projectile-file-path-with-line-column))
(progn
(kill-new file-path)
(message "%s" file-path))
(message "WARNING: Current buffer is not visiting a file!")))