setup-path.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ;;; Code:
  2. (require 'exec-path-from-shell)
  3. (defvar core-dir (concat user-emacs-directory "core")
  4. "All essencial settings.")
  5. (defvar mode-dir (concat user-emacs-directory "modes")
  6. "My config for Emacs modes.")
  7. (defvar site-elisp-dir (concat user-emacs-directory "site-elisp")
  8. "Extenal Emacs libs/modes.")
  9. (defvar utils-dir (concat user-emacs-directory "utils")
  10. "Useful functions to live in Emacs.")
  11. (defvar theme-dir (concat user-emacs-directory "themes")
  12. "My favorites templates.")
  13. (defvar distopico:load-path `(,core-dir ,mode-dir ,site-elisp-dir ,utils-dir ,theme-dir)
  14. "Directories relative to `user-emacs-directory', to be include in `load-path'.")
  15. (defvar distopico:exec-paths
  16. (list (concat user-emacs-directory "scripts")
  17. (concat user-emacs-directory "node_modules/.bin")
  18. (concat user-emacs-directory "virtualenv/bin"))
  19. "A list of custom bin paths.")
  20. (setq exec-path-from-shell-variables '("SSH_AGENT_PID" "SSH_AUTH_SOCK")
  21. exec-path-from-shell-arguments '("-l"))
  22. ;; General Paths
  23. (defun in-emacs-d (path)
  24. (expand-file-name (concat user-emacs-directory path)))
  25. (defun in-modes-d (path)
  26. (expand-file-name (concat mode-dir path)))
  27. (defun in-site-elisp-d (path)
  28. (expand-file-name (concat site-elisp-dir path)))
  29. ;; Functions
  30. (defun distopico:from-modes-d (path &optional symbol)
  31. (add-to-list 'load-path (in-modes-d path))
  32. (if symbol
  33. (require symbol)
  34. (load-library path)))
  35. (defun distopico:from-site-elisp-d (path &optional symbol)
  36. (add-to-list 'load-path (in-site-elisp-d path))
  37. (if symbol
  38. (require symbol)
  39. (load-library path)))
  40. (defun distopico:autoload-and-run (symbol file interactive callback)
  41. (autoload symbol file nil interactive)
  42. (eval-after-load (symbol-name symbol) callback))
  43. (defun distopico:path-join (&rest parts)
  44. (concat
  45. (mapconcat 'file-name-as-directory (butlast parts) "")
  46. (car (last parts))))
  47. (defun distopico:get-absolute-path (filename)
  48. (if (file-name-absolute-p filename)
  49. (expand-file-name filename)
  50. (distopico:path-join user-emacs-directory filename)))
  51. (defun distopico:get-path-subdirs (path-dir)
  52. "Return a list of `PATH-DIR' subdirectories."
  53. (mapcar (lambda (x) (distopico:path-join path-dir x))
  54. (cl-remove-if
  55. (lambda (x)
  56. (let ((abs-path (distopico:path-join path-dir x)))
  57. (or (string= x ".")
  58. (string= x "..")
  59. (not (file-accessible-directory-p abs-path)))))
  60. (directory-files path-dir))))
  61. (defun distopico:load-path-subdir(path-dir &optional path-exclude)
  62. (unless (file-exists-p path-dir)
  63. (make-directory path-dir))
  64. (if (< emacs-major-version 24)
  65. (progn
  66. (let ((base path-dir))
  67. (add-to-list 'load-path base)
  68. (dolist (f (directory-files base))
  69. (let ((name (concat base "/" f)))
  70. (when (and (file-directory-p name)
  71. (not (equal f ".."))
  72. (not (equal f ".")))
  73. (add-to-list 'load-path name))))))
  74. (progn
  75. (require 'cl)
  76. (require 'cl-lib)
  77. (let ((default-directory path-dir))
  78. (setq load-path
  79. (append
  80. (let ((load-path (copy-sequence load-path))) ;; Shadow
  81. (append
  82. (copy-sequence (normal-top-level-add-to-load-path '(".")))
  83. (normal-top-level-add-subdirs-to-load-path)))
  84. load-path))))))
  85. (defun distopico:to-list-path-subdir (path-dir appent-to)
  86. (add-to-list appent-to path-dir)
  87. (dolist (abs-dir (distopico:get-path-subdirs path-dir))
  88. (add-to-list appent-to abs-dir)))
  89. (defun distopico:startup-get-all-user-lisp-dirs ()
  90. "Return a list of absolute paths for all Lisp directories
  91. containing code under the control of the user."
  92. (let ((user-lisp-dirs ()))
  93. (dolist (path-dir distopico:load-path)
  94. (add-to-list 'user-lisp-dirs (expand-file-name path-dir))
  95. (dolist (list-paths (distopico:get-path-subdirs path-dir))
  96. (add-to-list 'user-lisp-dirs (expand-file-name list-paths))))
  97. (mapcar 'distopico:get-absolute-path user-lisp-dirs)))
  98. ;;;###autoload
  99. (defun distopico:startup-byte-recompile ()
  100. "Compile all packages in `distopico:load-path'."
  101. (interactive)
  102. (dolist (abs-dir (distopico:startup-get-all-user-lisp-dirs))
  103. (message abs-dir)
  104. (let ((generated-autoload-file (distopico:path-join abs-dir "loaddefs.el")))
  105. (update-directory-autoloads abs-dir))
  106. (byte-recompile-directory abs-dir 0)))
  107. ;;;###autoload
  108. (defun distopico:startup-load-path ()
  109. "Set enviroment variables in Emacs on startup."
  110. (interactive)
  111. (dolist (path-dir distopico:load-path)
  112. (distopico:load-path-subdir path-dir))
  113. (exec-path-from-shell-initialize)
  114. (setenv "PATH" (concat (getenv "PATH") ":" (mapconcat 'identity distopico:exec-paths ":") ))
  115. (setq exec-path (append exec-path distopico:exec-paths)))
  116. (provide 'setup-path)