-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly-cmake.el
116 lines (107 loc) · 4.08 KB
/
poly-cmake.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; poly-cmake.el --- Polymode for cmake -*- lexical-binding: t -*-
;;
;; Author: Chris Green
;; Maintainer: Chris Green
;; Copyright (C) 2021 Fermi Research Alliance, LLC.
;; Version: 0.1
;; Package-Requires: ((emacs "25") (polymode "0.2.2"))
;; URL: https://github.com/FNALssi/poly-cmake
;; Keywords: languages, multi-modes
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This file is *NOT* part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Polymode for allowing mode-specific editing of embedded regions in
;; the comments of a CMake file, the canonical example being
;; ReStructured Text blocks for documentation with Sphinx.
;;
;;; Known Bugs:
;;
;; * I have so far been unable to evoke desired indenting behavior with
;; respect to an embedded chunk of documentation when the enclosing
;; comment block is itself indented, viz:
;;
;; |function(my_cmake_function)
;; | # Do stuff
;; |
;; | #[============================================================[.rst:
;; |My documentation block should be flush-left unless I add spaces
;; |myself and <tab> should do the same thing it does in rst-mode.
;; | #]============================================================]
;; |
;; | # Moar stuff
;; |
;; |endfunction()
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'polymode)
(defun poly-cmake-mode-matcher ()
"Match mode for the code embedded in a CMake comment (#[=...[<x>:
1. Extension (e.g. x=`.rst') is looked-up in `auto-mode-alist'
2. Local value of `polymode-default-inner-mode'
3. `poly-fallback-mode'"
(let ((eol (point-at-eol)))
(save-excursion
(when (re-search-forward "\\[\\(\\.[[:alpha:]]+\\):" eol t)
(let ((str (match-string 1)))
(pm-get-mode-symbol-from-name str))))))
(define-auto-innermode poly-cmake-auto-innermode nil
"CMake auto-innermode to identify inner mode.
See `poly-cmake-mode-matcher' for how the mode of the chunk is
identified."
:head-matcher "^[ \t]*#\\[=*\\[\\.[[:alpha:]]+:\n"
:tail-matcher "^[ \t]*#\\]=*\\]$"
:fallback-mode 'text-mode
:head-mode 'host
:tail-mode 'host
:mode-matcher #'poly-cmake-mode-matcher)
(define-obsolete-variable-alias 'pm-host/cmake 'poly-cmake-hostmode "v0.2")
(define-hostmode poly-cmake-hostmode :mode 'cmake-mode)
;;;###autoload (autoload 'poly-cmake-mode "poly-cmake")
(define-polymode poly-cmake-mode
:hostmode 'poly-cmake-hostmode
:innermodes '(poly-cmake-auto-innermode)
:keymap '(("=" . poly-cmake-electric-eq)))
(defun poly-cmake-electric-eq (arg)
"Auto-insert an embedded chunk at ARG."
(interactive "P")
(if (or arg (car (pm-innermost-span)))
(self-insert-command (if (numberp arg) arg 1))
(if (not (looking-back "^\\([ \t]*\\)#\\[" nil))
(self-insert-command 1)
(let ((str (match-string 1)))
(insert "============================================================[.")
(save-excursion
(insert ":\n\n" str "#]============================================================]")
(unless(looking-at "\\s *$")
(newline)))))))
;;;###autoload
(setq auto-mode-alist
(append
'(("CMakeLists\\.txt\\'" . poly-cmake-mode))
'(("\\.cmake\\'" . poly-cmake-mode))
auto-mode-alist))
(provide 'poly-cmake)
;;; poly-cmake.el ends here