guix-env-var.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; guix-env-var.el --- Guix environment variables mode -*- lexical-binding: t -*-
  2. ;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides `guix-env-var-mode', the major mode for Guix
  18. ;; to prettify environment-variables and profile files.
  19. ;;; Code:
  20. (defgroup guix-env-var nil
  21. "Settings for `guix-env-var-mode'."
  22. :prefix "guix-env-var-"
  23. :group 'guix)
  24. (defcustom guix-env-var-enable-formatting t
  25. "If non-nil, prettify Guix environment-variables and profile files."
  26. :type 'boolean
  27. :group 'guix-env-var)
  28. (defvar guix-env-var-display-message t
  29. "If non-nil, display a message after enabling `guix-env-var-mode'.
  30. This message allows you to see that the current buffer is
  31. formatted (while the original buffer file may not be formatted).")
  32. (defvar guix-env-var-regexp
  33. (rx "export" (one-or-more space)
  34. letter (zero-or-more (or alphanumeric "_")) "=")
  35. "Regexp matching shell variables in Guix environment-variables file.")
  36. (defvar guix-env-var-comment-line-regexp
  37. (rx (zero-or-more whitespace) "#")
  38. "Regexp matching a line beginning with a comment symbol.")
  39. (defun guix-env-var-comment-line-p ()
  40. "Return non-nil if current line begins with a comment."
  41. (save-excursion
  42. (beginning-of-line)
  43. (re-search-forward guix-env-var-comment-line-regexp
  44. (line-end-position) t)))
  45. ;;;###autoload
  46. (defun guix-env-var-prettify-variable ()
  47. "Prettify shell environment variable at current line."
  48. (interactive)
  49. (let ((env-beg (line-beginning-position))
  50. (env-end (line-end-position)))
  51. (unless (guix-env-var-comment-line-p)
  52. (narrow-to-region env-beg env-end)
  53. (beginning-of-line)
  54. (when (search-forward "=" nil t)
  55. (insert "\\\n")
  56. (while (search-forward ":/" nil t)
  57. (backward-char 2)
  58. (insert "\\\n")
  59. (forward-char)))
  60. (end-of-line)
  61. (widen))))
  62. ;;;###autoload
  63. (defun guix-env-var-prettify-buffer (&optional buffer)
  64. "Prettify BUFFER with `guix-env-var-prettify-variable'.
  65. Interactively, or if BUFFER is not specified, use the current buffer."
  66. (interactive)
  67. (with-current-buffer (or buffer (current-buffer))
  68. (save-excursion
  69. (goto-char (point-min))
  70. (while (search-forward-regexp guix-env-var-regexp nil t)
  71. (guix-env-var-prettify-variable)))))
  72. ;;;###autoload
  73. (define-derived-mode guix-env-var-mode sh-mode "Guix-Env-Var"
  74. "Major mode for viewing Guix environment-variables and profile files.
  75. \\{guix-env-var-mode-map}"
  76. (when guix-env-var-enable-formatting
  77. (let ((inhibit-read-only t))
  78. (guix-env-var-prettify-buffer (current-buffer)))
  79. (set-buffer-modified-p nil)
  80. (when guix-env-var-display-message
  81. (message "This buffer has been formatted by `guix-env-var-mode'."))))
  82. (provide 'guix-env-var)
  83. ;;; guix-env-var.el ends here