-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from SalOrak/reduce-path-names-dict
feat(aliases): added aliases for better readability
- Loading branch information
Showing
1 changed file
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
;; Maintainer: Hector "Salorak" Alarcon <[email protected]> | ||
;; URL: https://github.com/salorak/whaler.el | ||
;; Version: 0.0.1 | ||
;; Package-Requires: ((emacs "25.1") ( f "0.20.0") ) | ||
;; Package-Requires: ((emacs "25.1") ( f "0.20.0") (dash "2.19.1") ) | ||
;; Keywords: tools | ||
|
||
;; This file is free software; you can redistribute it and/or modify | ||
|
@@ -29,9 +29,9 @@ | |
;; work with repositories / directories / projects. | ||
|
||
;; `Whaler' provides functions to easily create a pool of projects as | ||
;; well as interact with these projects. For example, with `Whaler' you | ||
;; well as interact with these projects. For example, with `Whaler' you | ||
;; can call `compile' in a project chosen from the minibuffer without | ||
;; changing the current project. But not only you can call `compile', but | ||
;; changing the current project. But not only you can call `compile', but | ||
;; any function or custom function you want, interacting either in your | ||
;; current working project or another project selected from the minibuffer. | ||
|
||
|
@@ -41,6 +41,7 @@ | |
(require 'cl-lib)) | ||
|
||
(require 'f) | ||
(require 'dash) | ||
|
||
;;* Customization | ||
(defgroup whaler nil | ||
|
@@ -90,19 +91,29 @@ DO NOT MODIFY IT MANUALLY, instead modify the `whaler-directories-alist' | |
and `whaler-oneoff-directories-alist' lists and then | ||
run `whaler-populate-projects-directories' to automatically update this list.") | ||
|
||
(defvar whaler--hash-project-aliases (make-hash-table :test 'equal) | ||
"Hash table containing the directory paths and its aliases. | ||
DO NOT MODIFY IT MANUALLY." | ||
) | ||
(defvar whaler--project-keys '() | ||
"List of KEYS for the `whaler--hash-project-aliases'. | ||
List used to be display in the `completing-read' function instead of the | ||
absolute PATH. It improves readability." | ||
) | ||
|
||
;; Functions | ||
(defun whaler-current-working-directory () | ||
"Returns the current working directory if set, otherwise the default directory. | ||
"Return the current working directory if set, otherwise the default directory. | ||
Fallbacks to the $HOME directory if neither is a valid path." | ||
(cond | ||
((and (stringp whaler-current-working-directory) | ||
(f-dir-p whaler-current-working-directory) | ||
(not (string-empty-p whaler-current-working-directory))) | ||
whaler-current-working-directory) | ||
whaler-current-working-directory) | ||
((and (stringp whaler-default-working-directory) | ||
(f-dir-p whaler-default-working-directory) | ||
(not (string-empty-p whaler-default-working-directory))) | ||
whaler-default-working-directory) | ||
whaler-default-working-directory) | ||
((f-dir-p (getenv "HOME")) | ||
(getenv "HOME")))) | ||
|
||
|
@@ -118,13 +129,13 @@ By default is t. | |
It accepts a string parameter, specifically the current whaler directory." | ||
(interactive) | ||
(let ((default-directory (whaler-current-working-directory))) | ||
(if (null action-arg) | ||
(funcall action) | ||
(funcall action default-directory)))) | ||
(if (null action-arg) | ||
(funcall action) | ||
(funcall action default-directory)))) | ||
|
||
|
||
(defun whaler--directory-exists (dir) | ||
"Checks whether the directory path exists or not. | ||
"Check whether the directory path exists or not. | ||
If DIR does not exist, prints it as an error message but doesn't raise an error." | ||
(if (not (f-dir-p dir)) | ||
(progn | ||
|
@@ -138,10 +149,10 @@ It search inside each directory in LIST argument and appends every subdirectory | |
in the `whaler--project-directories'. | ||
LIST corresponds to the list of directories to search in. | ||
HIDDEN is used to indicate whether to append hidden directories or not." | ||
(dolist (value list) | ||
(when (whaler--directory-exists value) | ||
(dolist (el (f-directories (f-long value) (lambda (x) (or (not (f-hidden-p x 'last)) hidden)) nil)) | ||
(add-to-list 'whaler--project-directories el))))) | ||
(dolist (value list) | ||
(when (whaler--directory-exists value) | ||
(dolist (el (f-directories (f-long value) (lambda (x) (or (not (f-hidden-p x 'last)) hidden)) nil)) | ||
(add-to-list 'whaler--project-directories el))))) | ||
|
||
(defun whaler--add-oneoff-directories () | ||
"Append the oneoff directories directly to the projects list." | ||
|
@@ -150,15 +161,54 @@ HIDDEN is used to indicate whether to append hidden directories or not." | |
(add-to-list 'whaler--project-directories x))) | ||
whaler-oneoff-directories-alist)) | ||
|
||
|
||
(defun whaler--format-path (path) | ||
"Return the PATH string formatted to be improve readability in the UI. | ||
The format is as follows: [ PARENT-DIRECTORY ] FINAL-DIRECTORY where | ||
PARENT-DIRECTORY is the parent directory of the PATH. | ||
FINAL-DIRECTORY is the directory pointing to. | ||
Example: | ||
Given the PATH '/home/user/programming/whaler' | ||
The PARENT-DIRECTORY is 'programming' and | ||
the FINAL-DIRECTORY is 'whaler'. | ||
The functions then returns '[ programming ] whaler' string." | ||
(cond | ||
(( not (f-root-p path)) | ||
(let ( | ||
(parent (car (last (f-split (f-parent path))))) | ||
(final (cadr (last (f-split path) 2)))) | ||
(format "[ %s ] %s" parent final))) | ||
(t | ||
(format "[ root ] /") | ||
))) | ||
|
||
(defun whaler--update-hash-table () | ||
"Update the projects hash table. | ||
The hash table contains the ALIAS as KEY and PATH as VALUE" | ||
(setq whaler--hash-project-aliases (make-hash-table :test 'equal)) | ||
(setq whaler--project-keys '()) | ||
(mapcar #'(lambda (path) | ||
(progn | ||
(let ((fp (whaler--format-path path))) | ||
(puthash fp path whaler--hash-project-aliases) | ||
(add-to-list 'whaler--project-keys fp) | ||
) | ||
)) | ||
whaler--project-directories) | ||
) | ||
|
||
(defun whaler-populate-projects-directories () | ||
"Populate the projects list `whaler--project-directories' and delete duplicates." | ||
(interactive) | ||
(setq whaler--project-directories '()) | ||
(whaler--generate-subdirectories whaler-directories-alist) | ||
(whaler--add-oneoff-directories) | ||
(delete-dups whaler--project-directories) | ||
(whaler--update-hash-table) | ||
(message "[Whaler] Projects have been repopulated.")) | ||
|
||
|
||
(cl-defun whaler ( &key (action 'dired) (action-arg t)(change-cwd-auto t)) | ||
"Change or move between directories blazingly fast. | ||
Apply an ACTION in the selected directory. | ||
|
@@ -171,17 +221,17 @@ ACTION-ARG determines whether the ACTION function should receive the | |
selected directory or not. By default is t. | ||
CHANGE-CWD-AUTO is a boolean indicating whether whaler should | ||
set the selected candidate as its current working directory or not. Default t" | ||
set the selected candidate as its current working directory or not. Default t" | ||
|
||
(interactive) | ||
(let ((chosen-directory "")) | ||
(setq chosen-directory (completing-read "[ Whaler ] >> " whaler--project-directories nil t)) | ||
(setq chosen-key (completing-read "[ Whaler ] >> " whaler--project-keys nil t)) | ||
(setq chosen-directory (gethash chosen-key whaler--hash-project-aliases)) | ||
(when change-cwd-auto (setq whaler-current-working-directory (f-slash chosen-directory))) | ||
(let ((default-directory chosen-directory)) | ||
(if (null action-arg) | ||
(funcall action) | ||
(funcall action chosen-directory))))) | ||
|
||
(funcall action) | ||
(funcall action chosen-directory))))) | ||
|
||
|
||
(provide 'whaler) | ||
|