Pandoc: how to add line numbers to code blocks with docx output - pandoc

I am trying to enable line numbers with docx output to no avail:
~~~~ {#mycode .haskell .numberLines startFrom="100"}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pandoc test.md -o build/test.docx
Is there anything special to do to enable line numbering for docx output?

Pandoc does not support this out of the box, and neither does Word (as far as I know).
One could try to use a filter to add line numbers directly into the source text. The filter could also create a numbered list, where each item contains a single line of code. However, this might result in issues with highlighting or indentation, YMMV.

Related

(Scheme) Unbound Variable when copy-pasting code

I'm copying the following Scheme code into a buffer file on emacs from a pdf:
(define (plural wd)
(if (equal? (last wd) ’y)
(word (bl wd) ’ies)
(word wd ’s)))
The initial formatting is as a long string, and I manually edit it to the format seen above. The file loads, but when I use the function I get the error:
*** Error:
unbound variable: |’y|
Current eval stack:
__________________
0 (equal? (last wd) |’y|)
1 (if (equal? (last wd) |’y|) (word (bl wd) |’ies|) (word wd |’s|))
When I manually type this code and load the file, however, the function runs no problem.
In what way is the pasting/editing of the code messing with the formatting of the code?
Is there a proper way to copy-paste code into a file? I tried formatting the code in a text editor before pasting into the buffer, but that didn't work either.
Thank you for your time and help.
It was already answered in the comments by Barmar, but this should enable you to complete your question, and help anybody else with the same problem in the future.
When you copy/pasted the code from the PDF, you did not copy a simple ASCII quote character '. Instead, you copied a "right single quotation mark" (unicode U+2019) ’. As this is not a reserved character in Scheme, it can be used as an identifier, and so what you expected to be the quoted symbol 'y was in fact the identifier ’y. The error was caused by there being no binding for the variable ’y.
A simple way of fixing this that does not required manually copying the code or fixing every quotation mark by hand is to find-and-replace ’ for ' (as long as you don't expect any ’ characters in your strings).

Regex not matching string in scheme but works on other platform

I am running string-match using the pattern [ \[\]a-zA-Z0-9_:.,/-]+ to match a sample text Text [a,b]. Although the pattern works on regex101, when I run it on scheme it returns #f. Here is the regex101 link.
This is the function I am running
(string-match "[ \\[\\]a-zA-Z0-9_:.,/-]+" "Text [a,b]")
Why isn't it working on scheme but works eleswhere? Am I missing something?
After discussing the issue on the guile gnu mailing list, I found out that Guile's (ice-9 regex) library uses POSIX extended regular expressions. And this flavor of regular expression doesn't support escaping in character classes [..], hence that's why it wasn't matching the strings.
However, I used the following function as a workaround and it works:
(string-match "[][a-zA-Z]+" "Text[ab]")
I don't see anything wrong with your regular expression syntax as it is quoted correctly so I assume there must be a bug in Guile, or the regexp library it uses, where \] just isn't interpreted the correct way inside brackets. I found a workaround by using the octal code point values instead:
(string-match "[A-Za-z\\[\\0135]+" "Text [a,b]")
; ==> #("Text [a,b]" (0 . 4))
Your regular expression isn't very good. It matches any combination of those chars so "]/Te,3.xt[2" also matches. If you are expecting a string like "Something [something, something]" I would rather have made /[A-Z][a-z0-9]+ [[a-z0-9]+,[a-z0-9]+]/ instead. eg.
(define pattern "[A-Z][a-z0-9]+ \\[[a-z0-9]+,[a-z0-9]+\\]")
(string-match pattern "Test [q,w]") ; ==> #("Test [q,w]" (0 . 10))
(string-match pattern "Be100 [sub,45]") ; ==> #("Be100 [sub,45]" (0 . 14))

Cascalog process multi-line json?

I have a directory of Json files that I want to process using cascalog. The solution I have right now requires me to remove all newline characters from my json files using a bash script. I am looking a better solution because I sync these files using rsync.
My question is can I read the contents of a file in Cascalog and return the contents of the file as one tuple. At present the function 'lfs-textline' returns a sequence of tuples for each line in the file, hence why I have to remove the newline characters. Preferably I want to return a sequence of tuples for each file.
(defn textline-parsed [dir]
(let [source (lfs-textline dir)]
(<- [?line]
(source ?line))))
Use hfs-wholefile from cascalog.more-taps to do this.
(:require [cascalog.more-taps :as taps])
(defn- byte-writable-to-str [bw]
"convert byte writable to stirng"
[(apply str (map char (. bw (getBytes))))])
And, use
(??<- [?str]
((taps/hfs-wholefile path) ?filename ?file-content)
(byte-writable-to-str ?file-content :> ?str)

How can I random sort lines in a buffer?

I have a buffer of words and phrases in sorted order and I would like to have the lines sorted in a random order. How would I do this with either an emacs builtin function or with elisp?
For example, given
bar
elisp
emacs
foo
hello world
the quick brown fox
I would like some completely random result like:
foo
the quick brown fox
hello world
elisp
emacs
bar
or ...
hello world
elisp
bar
the quick brown fox
foo
emacs
Using Bash on GNU/Linux:
Similar to Sean's solution, select the region and then:
C-u M-| shuf
Explanation:
M-| pipes the content of selected region to the bash command shuf. shuf shuffles the lines. The prefix C-u takes the output of shuf and uses it to overwrite the selected region.
Alternatively, here is sort-lines adapted to this requirement.
I've removed the reverse argument (obviously not relevant here), and simply supplied a 'comparison' function returning a random result to sort-subr.
(defun my-random-sort-lines (beg end)
"Sort lines in region randomly."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(let ;; To make `end-of-line' and etc. to ignore fields.
((inhibit-field-text-motion t))
(sort-subr nil 'forward-line 'end-of-line nil nil
(lambda (s1 s2) (eq (random 2) 0)))))))
For the original:
M-x find-function RET sort-lines RET
randomize-region.el seems to do what you want.
If you don't mind shelling out to Perl, you can select the region you want to randomize, and then type C-u M-| perl -MList::Util=shuffle -e 'print shuffle <STDIN>'.
I'm sure many other popular programming languages offer similar facilities.

When does format actually print in Common Lisp?

I have the following Common Lisp code:
(defun micro-read-eval-print ()
(format t "Micro > ")
(let ((form (read-line)))))
When I run it, I get the following:
CL-USER> (micro-read-eval-print)
(m-quote a)
Micro > NIL
Note that I typed in "(m-quote a)", while the Lisp interpreter output "Micro > NIL".
Now, I would have expected these events to happen in the reverse order. I would have expected "Micro > " to have been printed first since the format statement comes first. Why isn't it printed first? And what do I have to do to make sure it is printed first?
Try adding
(defun micro-read-eval-print ()
(format t "Micro > ")
(finish-output)
(let ((form (read-line)))))
I believe you are encountering the buffering of standard io (stdio) which, in C, is commonly bypassed via fflush() in that language.
finish-output appears to be the Common Lisp equivalent of C standard library's fflush.

Resources