Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.3 KiB

  1. ;;; sed-mode.el --- Major mode to edit sed scripts -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Version: 1.0
  5. ;; Keywords:
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; If you need this major mode, you might also want to
  18. ;; consider spending some time with `M-x doctor'.
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'smie)
  22. (defgroup sed-mode nil
  23. "Major mode to edit sed code."
  24. :group 'programming)
  25. (defvar sed-mode-syntax-table
  26. (let ((st (make-syntax-table)))
  27. (modify-syntax-entry ?# "<" st)
  28. (modify-syntax-entry ?\n ">" st)
  29. (modify-syntax-entry ?\\ "." st)
  30. st))
  31. (defconst sed-commands ":=aiqQrRbcdDhHgGlnNpPstTwWxy")
  32. (eval-and-compile
  33. (defconst sed-command-prefix-regexp "\\(?:^\\|[$/0-9;]\\)[ \t]*")
  34. (defconst sed-address-prefix-regexp "\\(?:^\\|[,;]\\)[ \t]*"))
  35. (defconst sed-label-regexp "[[:alnum:]]+")
  36. (defun sed-syntax-propertize (beg end)
  37. (goto-char beg)
  38. (sed-syntax-propertize-string end)
  39. (funcall
  40. (syntax-propertize-rules
  41. ("\\\\$"
  42. (0 (unless (nth 8 (save-excursion (syntax-ppss (match-beginning 0))))
  43. (put-text-property (match-beginning 0) (match-end 0)
  44. 'syntax-table (string-to-syntax "|"))
  45. (sed-syntax-propertize-string end)
  46. nil)))
  47. ((concat "\\(?:" sed-address-prefix-regexp
  48. "\\(?:\\(?1:/\\)\\|\\\\\\(?1:.\\)\\)"
  49. "\\|" sed-command-prefix-regexp "[sy]\\(?1:.\\)"
  50. "\\)")
  51. (0 (unless (nth 8 (save-excursion (syntax-ppss (match-beginning 0))))
  52. (put-text-property (match-beginning 1) (match-end 1)
  53. 'syntax-table (string-to-syntax "\""))
  54. (sed-syntax-propertize-string end)
  55. nil))))
  56. (point) end))
  57. (defun sed-syntax-propertize-string (end)
  58. (let* ((ppss (syntax-ppss))
  59. (c (nth 3 ppss)))
  60. (when c
  61. (let ((count (cond
  62. ((or (eq c t)
  63. (not (memq (char-before (nth 8 ppss)) '(?s ?y))))
  64. 1)
  65. (t 2))))
  66. (goto-char (1+ (nth 8 ppss)))
  67. (when (re-search-forward
  68. (if (eq c t) "[^\\]\n" (regexp-quote (string c)))
  69. end 'move count)
  70. (put-text-property (1- (match-end 0)) (match-end 0)
  71. 'syntax-table
  72. (if (eq c t) (string-to-syntax "|")
  73. (string-to-syntax "\""))))))))
  74. (defun sed--font-lock-command (cmd)
  75. (unless (nth 8 (syntax-ppss))
  76. (pcase cmd
  77. (?: (if (looking-at (concat "[ ]*\\(" sed-label-regexp "\\)"))
  78. (put-text-property (match-beginning 1) (match-end 1) 'face
  79. font-lock-function-name-face)))
  80. ((or ?b ?t ?T)
  81. (if (looking-at (concat "[ ]*\\(" sed-label-regexp "\\)"))
  82. (put-text-property (match-beginning 1) (match-end 1) 'face
  83. font-lock-constant-face))))
  84. font-lock-keyword-face))
  85. (defconst sed-font-lock-keywords
  86. `((,(concat sed-command-prefix-regexp "\\([" sed-commands "]\\)")
  87. (1 (sed--font-lock-command (char-after (match-beginning 1)))))))
  88. (defconst sed-smie-grammar nil)
  89. (defun sed-smie-rules (kind token)
  90. (pcase (cons kind token)
  91. (`(:list-intro . ,_) t)
  92. ))
  93. ;;;###autoload (add-to-list 'auto-mode-alist '("\\.sed\\'" . sed-mode))
  94. ;;;###autoload (add-to-list 'interpreter-mode-alist '("sed" . sed-mode))
  95. ;;;###autoload
  96. (define-derived-mode sed-mode prog-mode "Sed"
  97. "Sed editing mode."
  98. ;; (setq-local font-lock-support-mode nil) ;; To help debugging.
  99. (setq-local comment-start "# ")
  100. (setq-local comment-end "")
  101. (setq-local parse-sexp-lookup-properties t)
  102. (setq-local open-paren-in-column-0-is-defun-start nil)
  103. (setq-local syntax-propertize-function #'sed-syntax-propertize)
  104. (setq-local font-lock-defaults '(sed-font-lock-keywords))
  105. (smie-setup sed-smie-grammar #'sed-smie-rules
  106. ;; :backward-token #'sm-c-smie-backward-token
  107. ;; :forward-token #'sm-c-smie-forward-token
  108. )
  109. ;; Backslash auto-realign.
  110. ;; (add-hook 'after-change-functions #'sm-c--bs-after-change nil t)
  111. ;; (add-hook 'post-command-hook #'sm-c--bs-realign nil t)
  112. ;; (setq-local add-log-current-defun-header-regexp sm-c--def-regexp)
  113. ;; (setq-local imenu-generic-expression `((nil ,sm-c--def-regexp 1)))
  114. )
  115. ;;;; ChangeLog:
  116. ;; 2016-02-26 Stefan Monnier <monnier@iro.umontreal.ca>
  117. ;;
  118. ;; * sed-mode/sed-mode.el: Bump up version for first release
  119. ;;
  120. ;; (interpreter-mode-alist): Register ourselves.
  121. ;;
  122. ;; 2016-02-25 Stefan Monnier <monnier@iro.umontreal.ca>
  123. ;;
  124. ;; * sed-mode: New package
  125. ;;
  126. (provide 'sed-mode)
  127. ;;; sed-mode.el ends here