Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7bf71a

Browse files
authored
Merge pull request #64 from vindarel/master
Highlight lisp
2 parents ec9770c + 373d2f1 commit d7bf71a

File tree

10 files changed

+673
-103
lines changed

10 files changed

+673
-103
lines changed

‎README.org‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Each pull request is vetted to correctly compile in Jekyll; this is
5050
then logged on Github and has to pass for a pull request to be
5151
mergable to master.
5252

53+
We use [[https://github.com/orthecreedence/highlight-lisp][highlight-lisp]] for syntax highlighting. Just add a =lisp= class to
54+
your code samples.
5355

5456
** Notes regarding build process.
5557

‎_includes/header.html‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<script src="{{ site.baseurl }}/js/bootstrap.min.js"></script>
1010
<link href="{{ site.baseurl }}/css/bootstrap.min.css" rel="stylesheet">
1111
<link href="{{ site.baseurl }}/css/theme.css" rel="stylesheet">
12+
<script type="text/javascript" src="{{ site.baseurl }}/js/highlight-lisp.js"></script>
13+
<link href="{{ site.baseurl }}/css/highlight-lisp-github.css" rel="stylesheet">
1214
</head>
1315

1416
<body>
@@ -44,10 +46,13 @@
4446
{% endif %}
4547
</li>
4648
{% endfor %}
49+
</li>
4750

4851
</ul>
4952
</div>
5053

5154
</div>
5255
</div>
5356
</div>
57+
</body>
58+
</html>

‎_layouts/default.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
</div>
1414

1515
{% include footer.html %}
16+
17+
<script type="text/javascript">
18+
HighlightLisp.highlight_auto({className: 'lisp'});
19+
</script>

‎css/highlight-lisp-github.css‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Inspired by github's default code highlighting
3+
*/
4+
pre { white-space: pre; background-color: #f8f8f8; border: 1px solid #ccc; font-size: 13px; line-height: 19px; overflow: auto; padding: 6px 10px; border-radius: 3px; }
5+
pre code.hl-highlighted {white-space: pre; margin: 0; padding: 0; background: none; border: none; overflow-x: auto; font-size: 13px;}
6+
code.hl-highlighted {margin: 0 2px; padding: 0 5px; white-space: nowrap; font-family: Consolas, "Liberation Mono", Courier, monospace; background: #f8f8f8; border: 1px solid #eaeaea; border-radius: 3px;}
7+
8+
code.hl-highlighted {color: #008080;}
9+
code.hl-highlighted .function {color: #008080;}
10+
code.hl-highlighted .function.known {color: #800603;}
11+
code.hl-highlighted .function.known.special {color: #2d2d2d; font-weight: bold;}
12+
code.hl-highlighted .keyword {color: #990073;}
13+
code.hl-highlighted .keyword.known {color: #990073;}
14+
code.hl-highlighted .symbol {color: #75a;}
15+
code.hl-highlighted .lambda-list {color: #966;}
16+
code.hl-highlighted .number {color: #800;}
17+
code.hl-highlighted .variable.known {color: #c3c;}
18+
code.hl-highlighted .variable.global {color: #939;}
19+
code.hl-highlighted .variable.constant {color: #229;}
20+
code.hl-highlighted .nil {color: #f00;}
21+
code.hl-highlighted .list {color: #222;}
22+
23+
code.hl-highlighted .string, code.hl-highlighted .string * {color: #d14 !important;}
24+
code.hl-highlighted .comment,
25+
code.hl-highlighted .comment *,
26+
code.hl-highlighted .comment .string
27+
code.hl-highlighted .comment .string * {color: #aaa !important;}
28+
code.hl-highlighted .string .comment {color: #d14 !important;}
29+
30+
code.hl-highlighted .list.active {display: inline-block; background: #aefff7;}
31+

‎examples/snippets.html‎

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,116 @@ <h1 class="title">Snippets</h1>
77
</header>
88
<p>One of the great resources we can have as developers is snippets and example code to understand how the idiom of a language is spoken and written.</p>
99
<h3 id="defining-a-function">Defining a function:</h3>
10-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defun</span><span class="fu"> function-name </span>(param1 param2 &amp;key (keyword1 default-value))
11-
<span class="co">;; keywords are optional; optional positional parameters are also available. </span>
12-
<span class="co">;; implicit progn</span>
13-
)</code></pre>
10+
<pre class="sourceCode lisp"><code class="sourceCode lisp">(defun function-name (param1 param2 &amp;key (keyword1 default-value))
11+
;; keywords are optional; optional positional parameters are also available.
12+
;; implicit progn
13+
)
14+
</code></pre>
15+
1416
<h3 id="defining-a-method-on-a-type">Defining a method on a type:</h3>
15-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<spanclass="kw">defmethod</span><spanclass="fu">method-name </span>((param1 <spanclass="kw">class-name</span>))
16-
<spanclass="co">;; implicit progn</span>
17+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethodmethod-name ((param1 class-name))
18+
;; implicit progn
1719
)</code></pre>
20+
1821
<h3 id="defining-a-class">Defining a class</h3>
1922
<p>Note that <code>DEFCLASS</code> accessor functions for slots can be <code>SETF</code>&#8217;d and serve as both getters and setters for the slot.</p>
2023
<p><code>:INITARG</code> is the keyword used in <code>MAKE-INSTANCE</code> to denote the value of the initial argument (see below).</p>
2124
<p><code>:INITFORM</code> is the form used to initialize the slot. Without this, it defaults to <code>nil</code>. I favor using <code>nil</code>, <code>0</code>, <code>&quot;&quot;</code>, or <code>(error &quot;You must set slot &lt;slotname&gt; to a value&quot;)</code> as the usual initform set.</p>
2225
<p>Note that <code>(:documentation ...)</code> is the standard documentation mechanism, which can be viewed in the running image with <code>DESCRIBE</code> (at least in SBCL).</p>
23-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
24-
<spanclass="co">;; no superclass, no slots.</span>
25-
(<spanclass="kw">defclass</span><spanclass="fu">superclass-1 </span><spanclass="kw">nil</span><spanclass="kw">nil</span>)
26+
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">
27+
;; no superclass, no slots.
28+
(defclasssuperclass-1 nilnil)
2629

27-
(<spanclass="kw">defclass</span><spanclass="fu">my-class </span>(superclass<spanclass="dv">-1</span>)
30+
(defclassmy-class (superclass-1)
2831
((variable
2932
:accessor accessor-function
3033
:initarg :variable
3134
:initform form-for-initializition.)
3235
another-variable)
33-
(:documentation <span class="st">&quot;a class snippet!&quot;</span>))</code></pre>
36+
(:documentation &quot;a class snippet!&quot;))</code></pre>
37+
3438
<h3 id="creating-an-instance">Creating an instance</h3>
35-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">make-instance</span> &#39;my-class :variable value)</code></pre>
39+
<pre class="sourceCode commonlisp"><code class="sourceCode lisp">(make-instance &#39;my-class :variable value)</code></pre>
40+
3641
<h3 id="adding-a-constructor">Adding a constructor</h3>
3742
<p>This function runs after the instance is initialized.</p>
38-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defmethod</span><span class="fu"> initialize-instance </span>:after ((obj my-class) &amp;key)
39-
(<span class="kw">setf</span> (accessor-function obj) <span class="dv">10</span>))</code></pre>
43+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmethod initialize-instance :after ((obj my-class) &amp;key)
44+
(setf (accessor-function obj) 10))</code></pre>
45+
4046
<h3 id="defining-a-constant">Defining a constant:</h3>
4147
<p>Note that the convention is that constants are surrounded with +</p>
42-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defconstant</span><span class="fu"> +name+ </span>value <span class="st">&quot;docstring&quot;</span>)</code></pre>
48+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defconstant +name+ value &quot;docstring&quot;)</code></pre>
49+
4350
<h3 id="defining-a-global-variable">Defining a global variable:</h3>
4451
<p>Note that the convention is that globals are surrounded with *</p>
45-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<span class="kw">defparameter</span><span class="fu"> *name* </span>value <span class="st">&quot;docstring&quot;</span>)</code></pre>
52+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defparameter *name* value "docstring")</code></pre>
53+
4654
<h3 id="creating-local-variables.">Creating local variables.</h3>
47-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<spanclass="kw">let</span> ((variable1 value-form)
55+
<pre class="sourceCode lisp"><code class="sourceCode lisp">(let ((variable1 value-form)
4856
(variable2 value-again))
49-
<spanclass="co">;; implicit progn where variable[12] are valid</span>
57+
;; implicit progn where variable[12] are valid</span>
5058
)</code></pre>
59+
5160
<h3 id="loop">LOOP</h3>
5261
<p>LOOP is a contentious form in Common Lisp: some people love its imperative style, others hate it. Regardless, its really handy! Here are my favorite LOOPs</p>
5362
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
54-
(<spanclass="kw">loop</span> for i from <spanclass="dv">0</span> upto <spanclass="dv">10</span>
63+
(loop for i from 0 upto 10
5564
collect i)
5665

57-
(<spanclass="kw">loop</span> for i from <spanclass="dv">0</span> below <spanclass="dv">10</span>
66+
(loop for i from 0 below 10
5867
collect i)
5968

60-
(<spanclass="kw">loop</span> for i from <spanclass="dv">0</span> upto <spanclass="dv">10</span>
61-
<spanclass="kw">do</span>
69+
(loop for i from 0 upto 10
70+
do
6271
(side-effect i))
6372

64-
(<spanclass="kw">loop</span> for ele in <spanclass="kw">list</span>
73+
(loop for ele in list
6574
collect
6675
(operate-on ele))
6776

68-
(<spanclass="kw">loop</span> for ele across <spanclass="kw">array</span>
77+
(loop for ele across array
6978
collect
7079
(operate-on ele))</code></pre>
7180
<h3 id="lambda-functions">Lambda functions</h3>
7281
<p>The lambda functions is an anonymous function, i.e., unnamed.</p>
7382
<p>Here we map over <code>numeric-list</code> and increment each element in it by 1 with <code>1+</code>, returning the incremented list.</p>
7483
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
75-
(<spanclass="kw">mapcar</span>
76-
#&#39;(<spanclass="kw">lambda</span> (x)
77-
(<spanclass="kw">1+</span> x))
84+
(mapcar
85+
#&#39;(lambda (x)
86+
(1+ x))
7887
numeric-list)</code></pre>
7988
<h3 id="macro">Macro</h3>
8089
<p>Note that Lisp macros should be used with care: they read source code and emit source code.</p>
81-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<spanclass="kw">defmacro</span><spanclass="fu">with-resource </span>((resource) &amp;body body)
90+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defmacrowith-resource ((resource) &amp;body body)
8291
(allocate-resource ,resource)
83-
(<spanclass="kw">unwind-protect</span>
84-
(<spanclass="kw">progn</span>
92+
(unwind-protect
93+
(progn
8594
,@body)
8695
(deallocate-resource ,resource)))</code></pre>
8796
<p>See <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm">UNWIND-PROTECT</a> for details on this very useful form.</p>
8897
<h3 id="writing-a-text-file">Writing a text file</h3>
8998
<p>More complete reading and writing of text files can be done by using the ALEXANDRIA library; these routines are great for starting to customize your own routine.</p>
90-
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(<spanclass="kw">defun</span><spanclass="fu">write-file </span>(filename data)
91-
(<spanclass="kw">with-open-file</span> (<spanclass="kw">stream</span>
99+
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">(defunwrite-file (filename data)
100+
(with-open-file (stream
92101
filename
93-
<spanclass="kw">:direction</span><spanclass="kw">:output</span>
94-
:if-exists <spanclass="kw">:supersede</span>
95-
:if-does-not-exist <spanclass="kw">:create</span>)
96-
(<spanclass="kw">write-sequence</span>
102+
:direction:output
103+
:if-exists :supersede
104+
:if-does-not-exist :create)
105+
(write-sequence
97106
data
98-
<spanclass="kw">stream</span>)))</code></pre>
107+
stream)))</code></pre>
99108
<h3 id="reading-a-text-file">Reading a text file</h3>
100109
<pre class="sourceCode commonlisp"><code class="sourceCode commonlisp">
101-
(<spanclass="kw">defun</span><spanclass="fu">read-file </span>(filename)
102-
<spanclass="st">&quot;Reads `filename` as a sequence of unsigned 8-bit bytes, no</span>
103-
<spanclass="st">encoding&quot;</span>
104-
(<spanclass="kw">with-open-file</span> (fin filename
105-
<spanclass="kw">:direction</span><spanclass="kw">:input</span>
106-
:if-does-not-exist <spanclass="kw">:error</span>)
107-
(<spanclass="kw">let</span> ((seq (<spanclass="kw">make-array</span> (<spanclass="kw">file-length</span> fin)
108-
:fill-pointer <spanclass="kw">t</span>)))
109-
(<spanclass="kw">setf</span> (<spanclass="kw">fill-pointer</span> seq)
110-
(<spanclass="kw">read-sequence</span> seq fin))
110+
(defunread-file (filename)
111+
&quot;Reads `filename` as a sequence of unsigned 8-bit bytes, no
112+
encoding&quot;
113+
(with-open-file (fin filename
114+
:direction:input
115+
:if-does-not-exist :error)
116+
(let ((seq (make-array (file-length fin)
117+
:fill-pointer t)))
118+
(setf (fill-pointer seq)
119+
(read-sequence seq fin))
111120
seq)))</code></pre>
112121
<p>Please feel free to contribute your examples or library information to this page! Send in those pull requests and file those bugs!</p>
113122
<hr/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /