-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-feed
1532 lines (1321 loc) · 72.6 KB
/
rss-feed
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<rss version="2.0"><channel><title>MonadicT</title><link>http://MonadicT.github.io</link><description></description><item><title>Writing a parser using Parsec</title><link>http://MonadicT.github.io/2016/11/27/ParsecParser/</link><pubDate>Sun, 27 Nov 2016 00:00:00 -0800</pubDate><description>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Introduction</h2>
<div class="outline-text-2" id="text-1">
<p>
I need a parser for a simple Domain Specific Language and I am writing
it in Haskell using the Parsec combinator library. My future posts
will show why I am building this parser but in this post, I will focus
on how to construct a parser for an imperative language using Parsec.
</p>
<p>
I will make no attempt to explain Haskell features except to touch
upon some details of applicatives. There are many resources available on the
Internet for <a href="http://bfy.tw/Gbf">learning Haskell</a>
</p>
<p>
The implementation will be using Applicatives which will be easier to
read than one written in a monadic approach. The language I intend to
parse is a context-free grammar (CFG) and applicatives will do just
fine. It is helpful to review type signatures for <code>&lt;$&gt;</code>, <code>&lt;*&gt;</code>,
<code>*&gt;</code> and <code>&lt;*</code> shown below.
</p>
<div class="org-src-container">
<pre class="src src-haskell">:t (&lt;$&gt;)
(&lt;$&gt;) :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
:t (&lt;*&gt;)
(&lt;*&gt;) :: Applicative f =&gt; f (a -&gt; b) -&gt; f a -&gt; f b
:t (&lt;*)
(&lt;*) :: Applicative f =&gt; f a -&gt; f b -&gt; f a
:t (*&gt;)
(*&gt;) :: Applicative f =&gt; f a -&gt; f b -&gt; f b
</pre>
</div>
<p>
<code>&lt;$&gt;</code> applies a function <code>(a -&gt; b)</code> to an argument <code>(f a)</code> in a
computational context and produces a new value <code>(f b)</code>.
</p>
<p>
<code>&lt;*&gt;</code> extracts both the function <code>(a -&gt; b)</code> and the argument from
context <code>(f a)</code> producing a new value in <code>(f b)</code>.
</p>
<p>
<code>&lt;*</code> always returns the first argument and <code>*&gt;</code> the second.
</p>
<p>
The syntax of the language that our parser will recognize is shown below.
</p>
<pre class="example">
script :: sequence of stmts
stmt :: var_decl
| if_stmt
| while_stmt
| for_stmt
| continue_stmt
| break_stmt
| "{" stmt... "}"
var_decl :: ident ":=" expr
bool :: "true" | "false"
expr :: bexpr bool_op bexpr
| bexpr
bexpr :: rexpr relop rexpr
| rexpr
rexpr :: term termOp term
| term
term :: term factor_op factor
| factor
factor :: ID
| number
| string
| True
| False
| "(" expr ")"
| "+|-" factor
| ID "(" [exppr ["," expr]] ")"
term_op :: "+" | "-"
factor_op :: "*" | "/"
if_stmt :: "if" expr stmt | "if" bool_expr stmt "else" stmt
while_stmt :: "while" expr stmt
for_stmt :: "for" ID stmt
break_stmt :: "break"
continue_stmt :: "continue"
</pre>
<p>
The result of parsing will be an abstract syntax tree (AST). In
further posts, I will implement evaluation of the tree or use it
generate code.
</p>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Implementation</h2>
<div class="outline-text-2" id="text-2">
<p>
Using Parsec in applicative style leads to remarkably concise and
simple implementation.
</p>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">Module declaration</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Lists exports from this module. We export lower-level parse functions
for testing purposes.
</p>
<div class="org-src-container">
<pre class="src src-haskell">module Parser (Expr(..), Stmt(..), dslP, parse, exprP, stmtP) where
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2">Imports</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Notable imports are Parsec and friends. We also import some functions
from <code>Control.Applicative</code>.
</p>
<div class="org-src-container">
<pre class="src src-haskell">import Control.Applicative (liftA2, pure, (&lt;*&gt;), (&lt;$&gt;), (&lt;*), (*&gt;))
import Text.Parsec
import Text.Parsec.String (Parser)
import Text.ParserCombinators.Parsec.Char (digit, letter,
alphaNum, lower, upper)
import Text.ParserCombinators.Parsec.Language (emptyDef)
import qualified Text.ParserCombinators.Parsec.Token as Token
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3">
<h3 id="sec-2-3">Lexer</h3>
<div class="outline-text-3" id="text-2-3">
<div class="org-src-container">
<pre class="src src-haskell">tokenDef = Token.makeTokenParser $ emptyDef
{ Token.commentStart = "/*"
, Token.commentEnd = "*/"
, Token.commentLine = "//"
, Token.identStart = letter
, Token.identLetter = alphaNum
, Token.reservedNames = [ "break"
, "continue"
, "else"
, "false"
, "if"
, "print"
, "true"
, "while"
]
, Token.reservedOpNames = ["+", "-", "*", "/", ":="
, "&lt;", "&gt;", "|"
, "and", "or", "not"]
}
reserved = Token.reserved tokenDef
reservedOp = Token.reservedOp tokenDef
ident = Token.identifier tokenDef
integer = Token.integer tokenDef
float = Token.float tokenDef
stringLit = Token.stringLiteral tokenDef
ws = Token.whiteSpace tokenDef
symbol = Token.symbol tokenDef
parens = Token.parens tokenDef
braces = Token.braces tokenDef
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-4" class="outline-3">
<h3 id="sec-2-4">Data types</h3>
<div class="outline-text-3" id="text-2-4">
<div class="org-src-container">
<pre class="src src-haskell">data Expr =
Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
| Eq Expr Expr
| Less Expr Expr
| Greater Expr Expr
| Le Expr Expr
| Ge Expr Expr
| Ne Expr Expr
| And Expr Expr
| Or Expr Expr
| Not Expr
| Neg Expr
| Call String [Expr]
| V String
| I Integer
| D Double
| S String
| T
| F
deriving (Show, Eq)
data Stmt =
Assign Expr Expr
| Block [Stmt]
| Print [Expr]
| If Expr Stmt (Maybe Stmt)
| While Expr Stmt
| Break
| Continue
deriving (Show, Eq)
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-5" class="outline-3">
<h3 id="sec-2-5">Useful combinators</h3>
<div class="outline-text-3" id="text-2-5">
<div class="org-src-container">
<pre class="src src-haskell">commaSep p = p `sepBy` (symbol ",")
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-6" class="outline-3">
<h3 id="sec-2-6">Expression parser</h3>
<div class="outline-text-3" id="text-2-6">
<p>
This is the expression parser. This accepts semantically invalid
expressions as there is no distinction between numerical, string and
boolean expressions. In a future post, I will implement a semantic
pass over the AST which will flag invalid expressions.
</p>
<div class="org-src-container">
<pre class="src src-haskell">exprP :: Parser Expr
--exprP = termP `chainl1` termopP
exprP = bexprP `chainl1` bopP
bexprP = rexprP `chainl1` relopP
rexprP = termP `chainl1` termopP
termP :: Parser Expr
termP = factorP `chainl1` factoropP
factorP :: Parser Expr
factorP = Not &lt;$&gt; (notP *&gt; factorP)
&lt;|&gt; Neg &lt;$&gt; (symbol "-" *&gt; factorP)
&lt;|&gt; symbol "+" *&gt; factorP
&lt;|&gt; D &lt;$&gt; try float
&lt;|&gt; I &lt;$&gt; try integer
&lt;|&gt; S &lt;$&gt; stringLit
&lt;|&gt; reserved "true" *&gt; return T
&lt;|&gt; reserved "false" *&gt; return F
&lt;|&gt; try callP
&lt;|&gt; V &lt;$&gt; ident
&lt;|&gt; parens exprP
relopP = (reservedOp "=" *&gt; return Eq
&lt;|&gt; reservedOp "&lt;" *&gt; return Less
&lt;|&gt; reservedOp "&gt;" *&gt; return Greater
&lt;|&gt; reservedOp "!=" *&gt; return Ne
&lt;|&gt; reservedOp "&lt;=" *&gt; return Le
&lt;|&gt; reservedOp "&lt;=" *&gt; return Ge)
bopP = symbol "|" *&gt; return Or
&lt;|&gt; symbol "&amp;" *&gt; return And
&lt;|&gt; reserved "or" *&gt; return Or
&lt;|&gt; reserved "and" *&gt; return And
termopP = symbol "+" *&gt; return Add
&lt;|&gt; symbol "-" *&gt; return Sub
factoropP = symbol "*" *&gt; return Mul
&lt;|&gt; symbol "/" *&gt; return Div
callP = Call &lt;$&gt; ident &lt;*&gt; parens (commaSep exprP)
notP = reservedOp "!" &lt;|&gt; reserved "not"
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-7" class="outline-3">
<h3 id="sec-2-7">Statement parser</h3>
<div class="outline-text-3" id="text-2-7">
<div class="org-src-container">
<pre class="src src-haskell">stmtP :: Parser Stmt
stmtP = assignP
&lt;|&gt; blockP
&lt;|&gt; printP
&lt;|&gt; try ifElseP
&lt;|&gt; ifP
&lt;|&gt; whileP
&lt;|&gt; breakP
&lt;|&gt; continueP
blockP = Block &lt;$&gt; braces (many stmtP)
printP = Print &lt;$&gt; (reserved "print" &gt;&gt; (commaSep exprP))
assignP = Assign &lt;$&gt; exprP &lt;*&gt; (reservedOp ":=" &gt;&gt; exprP)
ifP = If &lt;$&gt; ((reserved "if") &gt;&gt; exprP) &lt;*&gt; stmtP &lt;*&gt; return Nothing
ifElseP = If &lt;$&gt; ((reserved "if") &gt;&gt; exprP) &lt;*&gt; stmtP
&lt;*&gt; ((reserved "else") *&gt; (Just &lt;$&gt; stmtP))
whileP = While &lt;$&gt; (reserved "while" &gt;&gt; exprP) &lt;*&gt; stmtP
breakP = reserved "break" *&gt; return Break
continueP = reserved "continue" *&gt; return Continue
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-8" class="outline-3">
<h3 id="sec-2-8">DSL Parser</h3>
<div class="outline-text-3" id="text-2-8">
<div class="org-src-container">
<pre class="src src-haskell">dslP :: Parser [Stmt]
dslP = ws *&gt; many stmtP &lt;* eof
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-9" class="outline-3">
<h3 id="sec-2-9">Most problematic areas</h3>
<div class="outline-text-3" id="text-2-9">
<p>
With Parsec, it turns out to be important to order the parsers and
adorn them with <code>try</code>. This was most evident in <code>factorP</code>. Very
briefly, when there are two parsers one of which is a prefix of the
orher, the parser of the longer input should be listed first. If a
parser can fail after consuming some input, it should be wrapped in
<code>try</code> so that the next parser will be tried at the correct input
position.
</p>
<p>
Other than this, using Parsec to build parsers is pretty straightforward.
</p>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Test program</h2>
<div class="outline-text-2" id="text-3">
<p>
Here is test program that verifies the correctness of the parser. The
tokenizer seems to have a bug. It correctly parses "1.2" as <code>D 1.2</code>
but parses <code>-1.2</code> as <code>I (-1)</code>. I will defer this issue for now!
</p>
</div>
<div id="outline-container-sec-3-1" class="outline-3">
<h3 id="sec-3-1">Module imports</h3>
<div class="outline-text-3" id="text-3-1">
<div class="org-src-container">
<pre class="src src-haskell">import Text.Parsec (parseTest)
import Data.List (intercalate)
import Text.Parsec.String
import Parser
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-2" class="outline-3">
<h3 id="sec-3-2">Expression tests</h3>
<div class="outline-text-3" id="text-3-2">
<p>
Testing expression parsing.
</p>
<div class="org-src-container">
<pre class="src src-haskell">exprTests :: [(String, Expr)]
exprTests = [("10", I 10)
, ("-1", Neg (I 1))
, ("- 1", Neg (I 1))
, ("1.2", D 1.2)
, ("-1.2", Neg (D 1.2))
, ("- 1.3", Neg (D 1.3))
, ("a", V "a")
, ("\"a\"", S "a")
, ("true", T)
, ("false", F)
, ("1 + 2", Add (I 1) (I 2))
, ("1 + -2", Add (I 1) (Neg (I 2)))
, ("1 + 2 * 3", Add (I 1) (Mul (I 2) (I 3)))
, ("1 - 2", Sub (I 1) (I 2))
, ("1 - 2 * 3", Sub (I 1) (Mul (I 2) (I 3)))
, ("1 + 2 * 3 / 4", Add (I 1) (Div (Mul (I 2) (I 3)) (I 4)))
, ("1 + a", Add (I 1) (V "a"))
, ("1 = a", Eq (I 1) (V "a"))
, ("1 = 2", Eq (I 1) (I 2))
, ("true and true", And T T)
, ("true &amp; true", And T T)
, ("true | true", Or T T)
, ("true or true", Or T T)
, ("1 = 2 &amp; 2 = 4", And (Eq (I 1) (I 2)) (Eq (I 2) (I 4)))
, ("a = b &amp; c = d", And (Eq (V "a") (V "b")) (Eq (V "c") (V "d")))
, ("a = b | c = d", Or (Eq (V "a") (V "b")) (Eq (V "c") (V "d")))
, ("(a | b) &amp; (c | d)", And (Or (V "a") (V "b")) (Or (V "c") (V "d")))
, ("(a &amp; b) | (c &amp; d)", Or (And (V "a") (V "b")) (And (V "c") (V "d")))
, ("-(1.2)", Neg (D 1.2))
, ("+(1.2)", D 1.2)
, ("not true", Not T)
, ("not not true", Not (Not T))
, ("true = false", Eq T F)
, ("foo()", Call "foo" [])
, ("foo(1)", Call "foo" [I 1])
, ("foo(1, true)", Call "foo" [I 1, T])
, ("foo(1, 2)", Call "foo" [I 1, I 2])
]
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-3" class="outline-3">
<h3 id="sec-3-3">Statement tests</h3>
<div class="outline-text-3" id="text-3-3">
<p>
Testing statement parsing.
</p>
<div class="org-src-container">
<pre class="src src-haskell">stmtTests :: [(String, Stmt)]
stmtTests = [ ("x := 1", Assign (V "x") (I 1))
, ("print 1, 2", Print [I 1, I 2])
, ("print 1", Print [I 1])
, ("{}", Block [])
, ("if true print \"T\" else print \"F\"",
If T (Print [S "T"]) (Just (Print [S "F"])))
, ("if true print 1", If T (Print [I 1]) Nothing)
, ("break", Break)
, ("continue", Continue)
]
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-4" class="outline-3">
<h3 id="sec-3-4">Dsl tests</h3>
<div class="outline-text-3" id="text-3-4">
<p>
Example DSL and its expected result.
</p>
<div class="org-src-container">
<pre class="src src-haskell">dslTests :: [(String, [Stmt])]
dslTests = [ ("x := 1 y:= 2", [Assign (V "x") (I 1), Assign (V "y") (I 2)])
, (" x := 1 ", [Assign (V "x") (I 1)])
]
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-5" class="outline-3">
<h3 id="sec-3-5">Test runner</h3>
<div class="outline-text-3" id="text-3-5">
<p>
<code>testParser</code> accepts a list of input and expected results of parsing
them. Inputs that don't produce the expected results are printed.
</p>
<div class="org-src-container">
<pre class="src src-haskell">testParser:: (Eq a, Show a) =&gt; Parser a -&gt; [(String, a)] -&gt; IO ()
testParser p tests = do
putStr (intercalate "\r\n"
(filter (not . null)
(map
(\(s, e, r) -&gt; case r of
Right ast -&gt; if e == ast
then "" -- "Parsed: " ++ s
else "Error: " ++ s ++ " Exp: " ++
show e ++ " Act: " ++ show ast
Left e -&gt; "Parse error: \n" ++ show e)
(map (\(s, e) -&gt; (s, e, parse p s s)) tests))))
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-6" class="outline-3">
<h3 id="sec-3-6">Main program</h3>
<div class="outline-text-3" id="text-3-6">
<p>
Runs all tests defined above.
</p>
<div class="org-src-container">
<pre class="src src-haskell">main :: IO ()
main = do
testParser exprP exprTests
testParser stmtP stmtTests
testParser dslP dslTests
putStr "\nDONE!\n"
</pre>
</div>
</div>
</div>
</div>
</description></item><item><title>Blogging with emacs and org-mode</title><link>http://MonadicT.github.io/2016/06/19/Blogging/</link><pubDate>Sun, 19 Jun 2016 00:00:00 -0700</pubDate><description><p>
I do most of my work in <code>emacs</code> and blogging is no
exception. But, everytime I start a blog post, I go through a small
learning curve around the tools I use. I am going to put an end to it,
once and for all :)
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Emacs setup</h2>
<div class="outline-text-2" id="text-1">
<p>
Obviously, you need <code>org-mode</code>. Version <code>8.*</code> is preferred and there
are many breaking changes from its previous versions. If you are using
a recent vintage <code>emacs</code>, <code>org-mode</code> is already bundled. Unless you
are running into a bug involving advanced usage, there should be no
need to upgrade <code>org-mode</code>. But, I do recommend cloning repo
<code>org-mode</code> and installing it from the source. It's really fun to
experience how smooth building it is compared to some of our internal
software!
</p>
<p>
We need to tell <code>org-mode</code> what source languages to process for export
and whether it should prompt before processing a source block written
in some language. We do that by customizing <code>org-babel-load-langauges</code>
variable. We redefine <code>org-confirm-babel-evaluate</code> to skip asking for
confirmation for the languages.
</p>
<p>
We also set <code>org-src-fontify-natively</code> so that the source blocks are
fontified as well.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span style="color: #008787; font-weight: bold; font-style: italic;">;; </span><span style="color: #008787; font-weight: bold; font-style: italic;">Load org export extensions</span>
(<span style="color: #ff5f00; font-weight: bold;">require</span> '<span style="font-weight: bold; text-decoration: underline;">ox-latex</span>)
<span style="color: #008787; font-weight: bold; font-style: italic;">;; </span><span style="color: #008787; font-weight: bold; font-style: italic;">org-mode should process these languages</span>
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(python . t)
(clojure . t)
(latex . t)))
<span style="color: #008787; font-weight: bold; font-style: italic;">;; </span><span style="color: #008787; font-weight: bold; font-style: italic;">We don't want the "confirm evaluation" prompt for these languages</span>
(<span style="color: #ff5f00; font-weight: bold;">defun</span> <span style="color: #d7af00; font-weight: bold;">my-org-confirm-babel-evaluate</span> (lang body)
(not (<span style="color: #ff5f00; font-weight: bold;">or</span> (string= lang <span style="color: #afafff; font-style: italic;">"python"</span>)
(string= lang <span style="color: #afafff; font-style: italic;">"emacs-lisp"</span>)
(string= lang <span style="color: #afafff; font-style: italic;">"dot"</span>)
(string= lang <span style="color: #afafff; font-style: italic;">"latex"</span>))))
(<span style="color: #ff5f00; font-weight: bold;">setq</span> org-confirm-babel-evaluate 'my-org-confirm-babel-evaluate)
(<span style="color: #ff5f00; font-weight: bold;">setq</span> org-src-fontify-natively t)
</pre>
</div>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1">Source blocks</h3>
<div class="outline-text-3" id="text-1-1">
<p>
Source code blocks are delimited by <code>#+BEGIN_SRC lang</code> and
<code>#+END_SRC</code>. <code>emacs</code> evaluates the source block and uses the result in
export. Source block parameters, <code>:results</code> and <code>:exports</code> are
particularly useful in this context.
</p>
<p>
<code>:results</code> can have the following options specified.
</p>
<p>
collection is either <code>value</code> or <code>output</code>. The first option instructs
<code>org-mode</code> to capture the return value of evaluation and use it in
exporting. The second option causes any output written to <code>STDOUT</code>
to be used as the value of the source block.
</p>
<p>
type is one of <code>table, vector</code>, <code>list</code>, <code>scalar</code>, <code>verbatim</code> or
<code>file</code>. By default, result is handled as a <code>table</code> or <code>scalar</code>
depending on the value. Results of type <code>scalar</code> or <code>verbatim</code> are
not converted to tables. A result type of <code>file</code> points to the file
where the result is stored and causes a file link to be inserted.
</p>
<p>
format is one of:
</p>
<ul class="org-ul">
<li><code>raw</code> results are inserted directly into the buffer
</li>
<li><code>org</code> results are enclosed in a <code>#+BEGIN_SRC org</code> block
</li>
<li><code>html</code> results are assumed to be <code>HTML</code> and enclosed in a <code>#+BEGIN_SRC html</code> block
</li>
<li><code>latex</code> results assumed to be LaTeX and are enclosed in a BEGIN<sub>LaTeX</sub> block
</li>
<li><code>code</code> result are assumed to be parsable code and are enclosed in a code block
</li>
<li><code>pp</code> result is converted to pretty-printed code and is enclosed in a code block.
</li>
<li><code>drawer</code> result is wrapped in a RESULTS drawer
</li>
</ul>
<p>
handling is one of
</p>
<ul class="org-ul">
<li><code>silent</code> results are not inserted into the buffer
</li>
<li><code>replace</code> any existing value is replaced with value
</li>
<li><code>append</code> result is appended to existing
</li>
<li><code>prepend</code> reslult is prepended to existing
</li>
</ul>
<p>
<code>:exports</code> can have one of the following values.
</p>
<ul class="org-ul">
<li><code>code</code> body of source block is inserted into the buffer
</li>
<li><code>results</code> result of source block evaluation is inserted into the buffer
</li>
<li><code>both</code> code and results are inserted into the buffer
</li>
<li><code>none</code> neither code nor results are inserted into the buffer
</li>
</ul>
<p>
Here is a Python source code block which exports code and results.
</p>
<pre class="example">
#+BEGIN_SRC python :results value :exports both
# A simple Python source block
return 42
#+END_SRC
</pre>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #008787; font-weight: bold; font-style: italic;"># </span><span style="color: #008787; font-weight: bold; font-style: italic;">A simple Python source block</span>
<span style="color: #ff5f00; font-weight: bold;">return</span> 42
</pre>
</div>
<pre class="example">
42
</pre>
<p>
Here is another Python source block whose output is captured and only the results is exported.
</p>
<pre class="example">
#+BEGIN_SRC python :results output :exports results
print "Hello, world!"
return 42
#+END_SRC
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Blog post</h2>
<div class="outline-text-2" id="text-2">
</div><div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">Header</h3>
<div class="outline-text-3" id="text-2-1">
<p>
A blog post begins with the following.
</p>
<pre class="example">
#+TITLE: Blogging with org-mode
#+tags: emacs org-mode blogging
#+options: toc:nil num:nil tex:t LaTeX:t
</pre>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2">Including a \LaTeX generated figure</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Here is a \LaTeX{} block to generate an image in the exported
document. By default, the result type is <b>latex</b> and the output is
wrapped in <code>#:BEGIN_LaTeX...#:END_LaTeX</code> block. Note that there are
no temporary graphics files generated at all.
</p>
<div class="org-src-container">
<pre class="src src-latex"><span style="color: #ff5f00; font-weight: bold;">\let\earth\relax</span>
<span style="color: #ff5f00; font-weight: bold;">\input</span>{<span style="color: #afd700; font-weight: bold;">eltex1</span>}
<span style="color: #ff5f00; font-weight: bold;">\begin</span>{<span style="color: #d7af00; font-weight: bold;">figure</span>}
<span style="color: #ff5f00; font-weight: bold;">\begin</span>{<span style="color: #d7af00; font-weight: bold;">center</span>}
<span style="color: #ff5f00; font-weight: bold;">\begin</span>{<span style="color: #d7af00; font-weight: bold;">picture</span>}(40,60)(0,0)
<span style="color: #ff5f00; font-weight: bold;">\grid</span>{10}{8}
<span style="color: #ff5f00; font-weight: bold;">\end</span>{<span style="color: #d7af00; font-weight: bold;">picture</span>}
<span style="color: #ff5f00; font-weight: bold;">\end</span>{<span style="color: #d7af00; font-weight: bold;">center</span>}
<span style="color: #ff5f00; font-weight: bold;">\end</span>{<span style="color: #d7af00; font-weight: bold;">figure</span>}
</pre>
</div>
<p>
For latex source code blocks, default value of <code>:results</code> is <code>output</code>
and <code>:exports</code> is <code>results</code>.
</p>
<p>
In contrast, the following block creates
a graphic file as specified by <code>:file</code> argument. <code>:results</code> is
specified as <code>output file raw</code> which causes the output to be
interpreted as a file link. Note the use of <code>raw</code> without which the
file link will be wrapped into a latex block.
</p>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3">
<h3 id="sec-2-3">Including a graphviz diagram</h3>
<div class="outline-text-3" id="text-2-3">
<p>
We need to have <i>graphviz</i> installed on the system. <i>emacs</i> should be
able to find <code>dot</code> command. As usual, Windows is a pain but <i>I have
become comfortably numb</i> :)
</p>
<p>
You might need to locate and copy <code>ob-dot.el</code> to a location where
<code>emacs</code> will find it.
</p>
<div class="figure">
<p><img src="dotfig.png" alt="dotfig.png" />
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Publishing</h2>
<div class="outline-text-2" id="text-3">
<p>
My blogging host is at <i>mondiact.github.io</i>. My Github user id is
<i>MonadicT</i> and the repo named
<code>https://github.com/MonadicT/MonadicT.github.io</code> is the source of
posts. Github runs a restricted version of <a href="https://jekyllrb.com">Jekyll</a> on
this repo and generates a static website which is published on
<i>github.io</i>. Unfortunately, the restricted version doesn't let us do
tags and a blog post without tags is not all that useful.
</p>
<p>
The workaround is to do all the processing locally and generate the
static website and push it to github. And, we tell github to not run
Jekyll on our repo. The presence of <code>.nojekyll</code> in the root directory
tells github to run Jekyll.
</p>
<p>
All we need is a way to generate the posts locally and make it
available. While it can be done with Jekyll running on our system, I
chose to use a tool called <a href="http://nakkaya.com/static.html">Static</a> implemented in Clojure. The
following bash command starts static and rebuilds the site when posts
change.
</p>
<div class="org-src-container">
<pre class="src src-bash">java -jar ../static/target/static-app.jar --watch
</pre>
</div>
<p>
When the post is complete, we need to commit all the files to git and push it to Github.
</p>
<div class="org-src-container">
<pre class="src src-bash">git status
git add &lt;new posts&gt;
git commit -am "Commit message"
git push
</pre>
</div>
<p>
Just like that, the new post appears on Github in all its glory.
</p>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">Troubleshooting</h2>
<div class="outline-text-2" id="text-4">
<p>
There are many moving parts in <code>org-mode</code> and when things go wrong,
debugging is taxing. Google is our friend and one particularly nasty
bug I hit was the messed up state of <code>org-mode</code>. This magical sequence,
<code>C-U M-x org-reload</code>, fixed my issue with "wrong type argument" error.
</p>
</div>
</div>
</description></item><item><title>Configuring xmonad</title><link>http://MonadicT.github.io/2015/05/20/xmonad/</link><pubDate>Wed, 20 May 2015 00:00:00 -0700</pubDate><description><p>
<a href="http://www.xmonad.org">xmonad</a> is a minimal tiling window manager. This post shows how to
setup xmnoad on a Ubuntu 14.04 machine and configure it in small
steps. xmonad configuration is done through a Haskell program and this
proves to be one of the major obstacles to using xmonad
effectively. On the other hand, if you can understand basic Haskell syntax,
there is not another window manager that I know of which is as
extensible as xmonad. xmonad is to window management as emacs is to,
well, let's stop right there! You get the idea!
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Installing xmonad</h2>
<div class="outline-text-2" id="text-1">
<p>
My usual sequence for setting up a Linux computer is to install Ubuntu
or something similar. The distribution you use really doesn't matter
since we are going to be avoiding all the bells and whistles that come
with each distribution. Let's assume that you have X installed on the
system and a desktop such as gnome or Kde. In my case, I installed
Ubuntu 14.04 and installed gnome desktop using <i>sudo apt-get install
gnome-desktop</i>. I then created a file called
<b>/usr/share/applications/xmonad.desktop</b> with the following
contents. With this in place, Gnome Display Manager gives me an
additional choice of window managers to run when I login.
</p>
<p>
There are many other ways to get xmonad to run as the window
manager. <b>~/.xessionrc</b> is another place where xmonad can be started
on login.
</p>
<div class="org-src-container">
<pre class="src src-bash">[Desktop Entry]
Type=Application
Name=Xmonad
Exec=xmonad
NoDisplay=true
X-GNOME-WMName=Xmonad
X-GNOME-Autostart-Phase=WindowManager
X-GNOME-Provides=windowmanager
X-GNOME-Autostart-Notify=true
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Configuring xmonad</h2>
<div class="outline-text-2" id="text-2">
<p>
xmonad runs happily with no custom configuration. However, it is going to be
a bland experience. Customizing xmonad requires you to have a
<b>~/.xmonad/xmonad.hs</b> file. Adding content to this file will be the
main focus of this post.
</p>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">Hello, xmonad.hs</h3>
<div class="outline-text-3" id="text-2-1">
<p>
This is the simplest configuration of XMonad window manager. It just
runs xmonad with <i>defaultConfig</i>. <i>defaultConfig</i> is just a record
containing xmonad configuration options. As we will see later, we can use Haskell's record
update syntax to change the options.
</p>
<div class="org-src-container">
<pre class="src src-haskell">import XMonad
main = xmonad defaultConfig
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2">Taking back our Alt key</h3>
<div class="outline-text-3" id="text-2-2">
<p>
If you are an <i>emacs</i> user, you are likely to be missing some favorite
Alt key combinations. Let's change XMonad key to the Window key, which
most of us happen to have on our keyboards. The following will tell
XMonad to use Windows key instead of Alt.
</p>
<div class="org-src-container">
<pre class="src src-haskell">import XMonad
main = xmonad defaultConfig {
modMask = mod4Mask
}
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3">
<h3 id="sec-2-3">Configuring XMonad for a Desktop Environment</h3>
<div class="outline-text-3" id="text-2-3">
<p>
A list of configuration options can be seen at <a href="http://xmonad.org/xmonad-docs/xmonad/XMonad-Core.html#t:XConfig">XConfig</a>. Generally, we
run Xmonad in a <a href="http://en.wikipedia.org/wiki/Desktop_environment">Desktop Environment</a>. In such cases, Xmonad
configuration should start with <a href="http://xmonad.org/xmonad-docs/xmonad-contrib/XMonad-Config-Desktop.html">Desktop Config</a>. You can use a generic
desktop configuration or specific to the environment you are
using. Since I am running a Gnome environment, my xmnoad.hs will be as
shown below.
</p>
<div class="org-src-container">
<pre class="src src-haskell">import XMonad
import XMonad.Config.Desktop
main = xmonad desktopConfig {
modMask = mod4Mask
}
</pre>
</div>
<p>
Restarting Xmonad will now an interesting effect. Our window is moved
down by some distance leaving some room for running toolbars. We can
try running <a href="http://en.wikipedia.org/wiki/GNOME_Panel">GNOME Panel</a> as the status bar. That, of course, is
sub-optimal for my work flow and I prefer something like <a href="http://linux.die.net/man/1/dzen2">dzen2</a>. dzen2
reads its standard input stream and displays it in the window it creates.
</p>
</div>
</div>