insert-shebang.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; insert-shebang.el --- Inserts shebang line automatically -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013 Sachin Patil
  3. ;; Author: Sachin Patil <isachin@iitb.ac.in>
  4. ;; URL: http://github.com/psachin/insert-shebang
  5. ;; Keywords: tools, convenience
  6. ;; Version: 0.9
  7. ;; Package-Requires: None
  8. ;; This file is NOT a part of GNU Emacs.
  9. ;; insert-shebang is free software distributed under the terms of the
  10. ;; GNU General Public License, version 3. For details, see the file
  11. ;; COPYING.
  12. ;;; Commentary:
  13. ;; Inserts shebang line automatically
  14. ;;; Install
  15. ;; (load "~/.emacs.d/insert-shebang.el")
  16. ;;; Code:
  17. (defgroup insert-shebang nil
  18. "Inserts shebang line automatically"
  19. :group 'extensions
  20. :link '(url-link :tag "Github" "https://github.com/psachin/insert-shebang")
  21. )
  22. (defcustom insert-shebang-flag nil
  23. "*If non-nil, add the root directory to the load path."
  24. :type 'boolean
  25. :group 'example)
  26. (defun get-extension-and-insert(filename)
  27. "Get extension from FILENAME and insert shebang.
  28. FILENAME is a buffer name from which the extension in extracted."
  29. (interactive "*")
  30. (let (myInterpreter val)
  31. ;; Create a hash table
  32. ;; Our 'key' as well as 'values' are of type 'string'
  33. ;; so we used :test 'equal to let elisp know that the function equal
  34. ;; should be used when testing whether a key exists.
  35. (setq myInterpreter (make-hash-table :test 'equal))
  36. ;; add new entry here (dictionary)
  37. (puthash "py" "python" myInterpreter)
  38. (puthash "sh" "bash" myInterpreter)
  39. (puthash "el" "emacs" myInterpreter)
  40. (puthash "pl" "perl" myInterpreter)
  41. (puthash "rb" "ruby" myInterpreter)
  42. ;; strip filename extension
  43. (setq file-extn (file-name-extension filename))
  44. ;; get value against the key
  45. (if (gethash file-extn myInterpreter)
  46. ;; if key exists in hashtable
  47. (progn
  48. ;; set variable val to value of key
  49. (setq val (gethash file-extn myInterpreter))
  50. ;; if buffer is new
  51. (if (= (point-min) (point-max))
  52. (with-current-buffer (buffer-name)
  53. (goto-char (point-min))
  54. (insert (format "#!%s %s" (executable-find "env") val))
  55. (newline))
  56. ;; if buffer has something, then
  57. (save-excursion
  58. (goto-char (point-min))
  59. ;; search for shebang pattern
  60. (if (integerp (re-search-forward "^#![ ]?\\([a-zA-Z_./]+\\)" 50 t))
  61. (message "File already have %s shebang line" val)
  62. ;; prompt user
  63. (if (y-or-n-p "File do not have shebang line, do you want to insert it now? ")
  64. (progn
  65. (with-current-buffer (buffer-name)
  66. (goto-char (point-min))
  67. (insert (format "#!%s %s" (executable-find "env") val))
  68. (newline))
  69. (message "Shebang inserted"))
  70. (progn
  71. (message "Leaving file as it is"))
  72. )))))
  73. ;; if key don't exists
  74. (progn
  75. (message "Can't guess file type"))
  76. )))
  77. (defun insert-shebang()
  78. "Calls get-get-extension-and-insert with argument as
  79. buffer-name"
  80. (interactive "*")
  81. (get-extension-and-insert(buffer-name)))
  82. ;; create hook
  83. (add-hook 'find-file-hook 'insert-shebang)
  84. ;; (remove-hook 'find-file-hook 'insert-shebang)
  85. (provide 'insert-shebang)
  86. ;;; insert-shebang.el ends here