pathify.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; pathify.el --- Symlink your scripts into a PATH directory
  2. ;; Copyright © 2015, 2016 Alex Kost
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Created: 19 May 2015
  5. ;; Version: 0.1
  6. ;; URL: https://gitlab.com/alezost-emacs/pathify
  7. ;; URL: https://github.com/alezost/pathify.el
  8. ;; Keywords: convenience
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This file provides some code for convenient symlinking files into
  21. ;; particular directory (`pathify-directory'). It is intended to be
  22. ;; used for symlinking your scripts (or other executable files) into
  23. ;; your "~/bin" directory (as you surely have it in your PATH
  24. ;; environment variable :-).
  25. ;; To install the package manually, add the following to your init file:
  26. ;;
  27. ;; (add-to-list 'load-path "/path/to/pathify-dir")
  28. ;; (autoload 'pathify "pathify" nil t)
  29. ;; (autoload 'pathify-dired "pathify" nil t)
  30. ;;
  31. ;; Optionally, set `pathify-directory' variable (if you do not use
  32. ;; "~/bin"):
  33. ;; Now you may use "M-x pathify-dired" command in a dired buffer to
  34. ;; pathify the marked files (or the current file if nothing is marked).
  35. ;; For more convenience, you may bind this command to some key, for
  36. ;; example:
  37. ;;
  38. ;; (eval-after-load 'dired
  39. ;; '(define-key dired-mode-map "P" 'pathify-dired))
  40. ;;; Code:
  41. (defgroup pathify nil
  42. "Make symlinks in a your favourite PATH directory."
  43. :group 'convenience)
  44. (defcustom pathify-directory "~/bin"
  45. "Directory where symlinks are created."
  46. :type 'string
  47. :group 'pathify)
  48. (defcustom pathify-make-symlink-function
  49. #'pathify-make-symlink-truename
  50. "Function used to make a symlink."
  51. :type '(choice (function-item pathify-make-symlink)
  52. (function-item pathify-make-symlink-truename)
  53. (function :tag "Another function"))
  54. :group 'pathify)
  55. (defcustom pathify-dired-confirm-function #'y-or-n-p
  56. "Function used to prompt about confirmation of pathifying."
  57. :type '(choice (function-item y-or-n-p)
  58. (function-item yes-or-no-p)
  59. (function :tag "Another function"))
  60. :group 'pathify)
  61. (defun pathify-make-symlink (filename linkname)
  62. "Make a symbolic link to FILENAME, named LINKNAME.
  63. Prompt for confirmation if the link already exists."
  64. (make-symbolic-link filename linkname 0))
  65. (defun pathify-make-symlink-truename (filename linkname)
  66. "Make a symbolic link to a true name of FILENAME, named LINKNAME.
  67. Prompt for confirmation if the link already exists."
  68. (make-symbolic-link (file-truename filename) linkname 0))
  69. ;;;###autoload
  70. (defun pathify (file)
  71. "Pathify FILE.
  72. Make symlink to FILE in `pathify-directory'."
  73. (interactive "fMake symlink to file: ")
  74. (or (file-directory-p pathify-directory)
  75. (error "Directory '%s' does not exist.
  76. Set `pathify-directory' variable"
  77. pathify-directory))
  78. (let ((target (expand-file-name (file-name-nondirectory
  79. (directory-file-name file))
  80. pathify-directory)))
  81. (funcall pathify-make-symlink-function
  82. file target)
  83. (message "Symlink '%s' has been created." target)))
  84. ;;;###autoload
  85. (defun pathify-dired (&optional arg)
  86. "Pathify all marked (or next ARG) files or the current file."
  87. (interactive "P")
  88. (require 'dired)
  89. (let* ((files (dired-map-over-marks (dired-get-filename) arg))
  90. (filenames (nreverse (mapcar #'dired-make-relative files))))
  91. (if (dired-mark-pop-up nil 'pathify filenames
  92. pathify-dired-confirm-function
  93. (concat "Pathify "
  94. (dired-mark-prompt arg filenames)))
  95. (mapc #'pathify files)
  96. (message "(Pathifying has been cancelled)"))))
  97. (provide 'pathify)
  98. ;;; pathify.el ends here