index.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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>Objective-C example</h2>
  119. <div><textarea id="objectivec-code">
  120. /*
  121. This is a longer comment
  122. That spans two lines
  123. */
  124. #import <Test/Test.h>
  125. @implementation YourAppDelegate
  126. // This is a one-line comment
  127. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  128. char myString[] = "This is a C character array";
  129. int test = 5;
  130. return YES;
  131. }
  132. </textarea></div>
  133. <h2>Java example</h2>
  134. <div><textarea id="java-code">
  135. import com.demo.util.MyType;
  136. import com.demo.util.MyInterface;
  137. public enum Enum {
  138. VAL1, VAL2, VAL3
  139. }
  140. public class Class<T, V> implements MyInterface {
  141. public static final MyType<T, V> member;
  142. private class InnerClass {
  143. public int zero() {
  144. return 0;
  145. }
  146. }
  147. @Override
  148. public MyType method() {
  149. return member;
  150. }
  151. public void method2(MyType<T, V> value) {
  152. method();
  153. value.method3();
  154. member = value;
  155. }
  156. }
  157. </textarea></div>
  158. <h2>Scala example</h2>
  159. <div><textarea id="scala-code">
  160. object FilterTest extends App {
  161. def filter(xs: List[Int], threshold: Int) = {
  162. def process(ys: List[Int]): List[Int] =
  163. if (ys.isEmpty) ys
  164. else if (ys.head < threshold) ys.head :: process(ys.tail)
  165. else process(ys.tail)
  166. process(xs)
  167. }
  168. println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
  169. }
  170. </textarea></div>
  171. <script>
  172. var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), {
  173. lineNumbers: true,
  174. matchBrackets: true,
  175. mode: "text/x-csrc"
  176. });
  177. var cppEditor = CodeMirror.fromTextArea(document.getElementById("cpp-code"), {
  178. lineNumbers: true,
  179. matchBrackets: true,
  180. mode: "text/x-c++src"
  181. });
  182. var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
  183. lineNumbers: true,
  184. matchBrackets: true,
  185. mode: "text/x-java"
  186. });
  187. var objectivecEditor = CodeMirror.fromTextArea(document.getElementById("objectivec-code"), {
  188. lineNumbers: true,
  189. matchBrackets: true,
  190. mode: "text/x-objectivec"
  191. });
  192. var scalaEditor = CodeMirror.fromTextArea(document.getElementById("scala-code"), {
  193. lineNumbers: true,
  194. matchBrackets: true,
  195. mode: "text/x-scala"
  196. });
  197. var mac = CodeMirror.keyMap.default == CodeMirror.keyMap.macDefault;
  198. CodeMirror.keyMap.default[(mac ? "Cmd" : "Ctrl") + "-Space"] = "autocomplete";
  199. </script>
  200. <p>Simple mode that tries to handle C-like languages as well as it
  201. can. Takes two configuration parameters: <code>keywords</code>, an
  202. object whose property names are the keywords in the language,
  203. and <code>useCPP</code>, which determines whether C preprocessor
  204. directives are recognized.</p>
  205. <p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
  206. (C), <code>text/x-c++src</code> (C++), <code>text/x-java</code>
  207. (Java), <code>text/x-csharp</code> (C#),
  208. <code>text/x-objectivec</code> (Objective-C),
  209. <code>text/x-scala</code> (Scala), <code>text/x-vertex</code>
  210. and <code>x-shader/x-fragment</code> (shader programs).</p>
  211. </article>