Theme Inspinia
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.

index.html 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!doctype html>
  2. <title>CodeMirror: C-like mode</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../../doc/docs.css">
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="../../addon/edit/matchbrackets.js"></script>
  8. <link rel="stylesheet" href="../../addon/hint/show-hint.css">
  9. <script src="../../addon/hint/show-hint.js"></script>
  10. <script src="clike.js"></script>
  11. <style>.CodeMirror {border: 2px inset #dee;}</style>
  12. <div id=nav>
  13. <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
  14. <ul>
  15. <li><a href="../../index.html">Home</a>
  16. <li><a href="../../doc/manual.html">Manual</a>
  17. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  18. </ul>
  19. <ul>
  20. <li><a href="../index.html">Language modes</a>
  21. <li><a class=active href="#">C-like</a>
  22. </ul>
  23. </div>
  24. <article>
  25. <h2>C-like mode</h2>
  26. <div><textarea id="c-code">
  27. /* C demo code */
  28. #include <zmq.h>
  29. #include <pthread.h>
  30. #include <semaphore.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <malloc.h>
  35. typedef struct {
  36. void* arg_socket;
  37. zmq_msg_t* arg_msg;
  38. char* arg_string;
  39. unsigned long arg_len;
  40. int arg_int, arg_command;
  41. int signal_fd;
  42. int pad;
  43. void* context;
  44. sem_t sem;
  45. } acl_zmq_context;
  46. #define p(X) (context->arg_##X)
  47. void* zmq_thread(void* context_pointer) {
  48. acl_zmq_context* context = (acl_zmq_context*)context_pointer;
  49. char ok = 'K', err = 'X';
  50. int res;
  51. while (1) {
  52. while ((res = sem_wait(&amp;context->sem)) == EINTR);
  53. if (res) {write(context->signal_fd, &amp;err, 1); goto cleanup;}
  54. switch(p(command)) {
  55. case 0: goto cleanup;
  56. case 1: p(socket) = zmq_socket(context->context, p(int)); break;
  57. case 2: p(int) = zmq_close(p(socket)); break;
  58. case 3: p(int) = zmq_bind(p(socket), p(string)); break;
  59. case 4: p(int) = zmq_connect(p(socket), p(string)); break;
  60. case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &amp;p(len)); break;
  61. case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break;
  62. case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break;
  63. case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break;
  64. case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break;
  65. }
  66. p(command) = errno;
  67. write(context->signal_fd, &amp;ok, 1);
  68. }
  69. cleanup:
  70. close(context->signal_fd);
  71. free(context_pointer);
  72. return 0;
  73. }
  74. void* zmq_thread_init(void* zmq_context, int signal_fd) {
  75. acl_zmq_context* context = malloc(sizeof(acl_zmq_context));
  76. pthread_t thread;
  77. context->context = zmq_context;
  78. context->signal_fd = signal_fd;
  79. sem_init(&amp;context->sem, 1, 0);
  80. pthread_create(&amp;thread, 0, &amp;zmq_thread, context);
  81. pthread_detach(thread);
  82. return context;
  83. }
  84. </textarea></div>
  85. <h2>C++ example</h2>
  86. <div><textarea id="cpp-code">
  87. #include <iostream>
  88. #include "mystuff/util.h"
  89. namespace {
  90. enum Enum {
  91. VAL1, VAL2, VAL3
  92. };
  93. char32_t unicode_string = U"\U0010FFFF";
  94. string raw_string = R"delim(anything
  95. you
  96. want)delim";
  97. int Helper(const MyType& param) {
  98. return 0;
  99. }
  100. } // namespace
  101. class ForwardDec;
  102. template <class T, class V>
  103. class Class : public BaseClass {
  104. const MyType<T, V> member_;
  105. public:
  106. const MyType<T, V>& Method() const {
  107. return member_;
  108. }
  109. void Method2(MyType<T, V>* value);
  110. }
  111. template <class T, class V>
  112. void Class::Method2(MyType<T, V>* value) {
  113. std::out << 1 >> method();
  114. value->Method3(member_);
  115. member_ = value;
  116. }
  117. </textarea></div>
  118. <h2>Java example</h2>
  119. <div><textarea id="java-code">
  120. import com.demo.util.MyType;
  121. import com.demo.util.MyInterface;
  122. public enum Enum {
  123. VAL1, VAL2, VAL3
  124. }
  125. public class Class<T, V> implements MyInterface {
  126. public static final MyType<T, V> member;
  127. private class InnerClass {
  128. public int zero() {
  129. return 0;
  130. }
  131. }
  132. @Override
  133. public MyType method() {
  134. return member;
  135. }
  136. public void method2(MyType<T, V> value) {
  137. method();
  138. value.method3();
  139. member = value;
  140. }
  141. }
  142. </textarea></div>
  143. <h2>Scala example</h2>
  144. <div><textarea id="scala-code">
  145. object FilterTest extends App {
  146. def filter(xs: List[Int], threshold: Int) = {
  147. def process(ys: List[Int]): List[Int] =
  148. if (ys.isEmpty) ys
  149. else if (ys.head < threshold) ys.head :: process(ys.tail)
  150. else process(ys.tail)
  151. process(xs)
  152. }
  153. println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
  154. }
  155. </textarea></div>
  156. <script>
  157. var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), {
  158. lineNumbers: true,
  159. matchBrackets: true,
  160. mode: "text/x-csrc"
  161. });
  162. var cppEditor = CodeMirror.fromTextArea(document.getElementById("cpp-code"), {
  163. lineNumbers: true,
  164. matchBrackets: true,
  165. mode: "text/x-c++src"
  166. });
  167. var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
  168. lineNumbers: true,
  169. matchBrackets: true,
  170. mode: "text/x-java"
  171. });
  172. var scalaEditor = CodeMirror.fromTextArea(document.getElementById("scala-code"), {
  173. lineNumbers: true,
  174. matchBrackets: true,
  175. mode: "text/x-scala"
  176. });
  177. var mac = CodeMirror.keyMap.default == CodeMirror.keyMap.macDefault;
  178. CodeMirror.keyMap.default[(mac ? "Cmd" : "Ctrl") + "-Space"] = "autocomplete";
  179. </script>
  180. <p>Simple mode that tries to handle C-like languages as well as it
  181. can. Takes two configuration parameters: <code>keywords</code>, an
  182. object whose property names are the keywords in the language,
  183. and <code>useCPP</code>, which determines whether C preprocessor
  184. directives are recognized.</p>
  185. <p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
  186. (C), <code>text/x-c++src</code> (C++), <code>text/x-java</code>
  187. (Java), <code>text/x-csharp</code> (C#),
  188. <code>text/x-scala</code> (Scala), <code>text/x-vertex</code>
  189. and <code>x-shader/x-fragment</code> (shader programs).</p>
  190. </article>