Meaning of ~| in Common Lisp format - character

The CLHS describes
22.3.1.4 Tilde Vertical-Bar: Page
This outputs a page separator character, if possible. ~n| does this n times.
I did not find much for page separator character. Trying it out with SBCL 2.0 on MacOS a page separator seems to be the newline (Ascii 0A). This would make it the same as ~%?
Was it something else in the long history of Common Lisp?

For me, the output is ^L - ASCII NP, which, when presented to a printer, finishes the current page and starts on the next page.

It's the #\page character:
CL-USER> #\page
#\Page
CL-USER> (describe *)
#\Page
[base-char]
Char-code: 12
Char-name: Page
CL-USER> (format nil "~|")
"^L"
CL-USER> (aref * 0)
#\Page

Related

How to highlight floating numbers and integers differently in .F90 files in Emacs?

How to highlight floating numbers and integers differently in .F90 files in Emacs? In my version 24.4.1, there is no difference between floating numbers and integers. How can I display them in different colors?
The default emacs fortran mode doesn't differentiate the font faces for floats and integers. However, you could use the highlight-numbers package (via M-x package-list-packages or https://github.com/Fanael/highlight-numbers).
Add the following to your .emacs to highlight floats (change the foreground and background colors as you see fit:
(add-hook 'fortran-mode-hook 'highlight-numbers-mode)
(add-hook 'after-init-hook
(lambda ()
(puthash 'fortran-mode
(rx (and symbol-start
(? "-")
(+ digit)
"."
(+ digit)
(*? any)
symbol-end))
highlight-numbers-modelist)
(set-face-attribute 'highlight-numbers-number nil
:foreground "gray60" :background "black")))
I posted this question two weeks ago. Now I have had my own solution. So, I am posting it here as a reference for others who might encountered the same.
First of all, I'd love to thank #Justin for answering before and suggesting the highlight-number-mode. His solution probably works. However, I have a slightly more complicated requirement, so I decided to do it differently.
Also, I'd have to admit that I'm a very beginner in programming. So, you might find some stupid lines in my codes. Feel free to let me know if you think there's a better way.
Here is my solution.
Install highlight-number-mode and parent-mode in emacs. (The second one is required by the first one.) In my case, they are installed under /~/.emcas.d/elpa/.
As far as I see, the original highlight-number-mode does not support f90-mode, and it highlights all numbers (eg. 2, 3.4, 8e-2) equally, by changing fond color. This does not meet my requirement. Specifically, I wish to highlight only floating numbers, and I denoting them either as 2. or .5 in my .f90 script. In Fortran 90, floats and integers have different division rules. So, I hope to visualize them differently in order to reduce the risk of introducing such bugs. Therefore, I changed the following part in the source code (highlight-numbers.el):
(defconst highlight-numbers-generic-regexp
(rx (or
"."
(? (and
symbol-start
digit
symbol-end)
"."
(* digit))))
"Generic regexp for number highlighting.
It is used when no mode-specific one is available.")
(defvar highlight-numbers-modelist
(copy-hash-table
(eval-when-compile
(let ((table (make-hash-table :test 'eq)))
(puthash 'f90-mode
(rx (or
"."
(? (and
symbol-start
(or (and (+ digit) (? (or "." "e" ".e" "E" ".E")) (+ digit))
(and (+ digit) (? (or "e-" ".e-" "E-" ".E-")) (+ digit))
(and (+ digit) "." (+ digit) (? (or "e" "E")) (+ digit))
(* digit))
symbol-end)
"."
(* digit))))
table)
If you compare the above code with the original one by Fanael Linithien, you will notice that I have added two ".". As Fanael said (in private discussions), this is probably due to that "." is taken as a punctuation in f90-mode in emacs. So, I have to modify the regexp and the table accordingly.
Once the above has been done, I put the line (add-hook 'f90-mode-hook 'highlight-numbers-mode) in my init.el to load this package via elpa.
Afterwards, the floats in my code will be highlighted, such as
example
And you can see the difference in floats and integers.
In the end, I'd add that I have been using this for quite a few days. No problems happen so far. So, I guess it is working! :)

Hyperlinks within org-babel source code blocks?

I'd like to have some hyperlinks on the comments of an org-babel source code block. My goal is to export a file as html and be able to track some references, as in the following minimal example:
#+BEGIN_SRC lisp
(princ "Hello World!") ;; [[stackoverflow.com/blabla1234][Got this from SO.]]
#+END_SRC
"Problem" is that links don't get embedded inside of source code blocks (which actually makes a lot of sense).
Is there a way of overriding this behaviour, or an alternative syntax to insert hyperlinks within src blocks?
It is probably not possible now (as of org-mode 8.3.4). The HTML export engine currently doesn't appear to have a mechanism for escaping protected characters. You should submit implement it or submit a feature request! (details)
Some workarounds:
Imitate the output with raw HTML
You can output raw HTML that would otherwise look like the source block and it will render with the link intact:
#+BEGIN_HTML
<pre class="src src-sh">
(princ "Hello World!") ;; Got this from SO.
</pre>
#+END_HTML
Prevent Substitution
If your code is free of greater than and less than symbols you may be able to prevent them from being substituted with
(setq org-html-protect-char-alist '(("&" . "&"))
or if that doesn't work:
(setq htmlize-basic-character-table
;; Map characters in the 0-127 range to either one-character strings
;; or to numeric entities.
(let ((table (make-vector 128 ?\0)))
;; Map characters in the 32-126 range to themselves, others to
;; &#CODE entities;
(dotimes (i 128)
(setf (aref table i) (if (and (>= i 32) (<= i 126))
(char-to-string i)
(format "&#%d;" i))))
;; Set exceptions manually.
(setf
;; Don't escape newline, carriage return, and TAB.
(aref table ?\n) "\n"
(aref table ?\r) "\r"
(aref table ?\t) "\t"
;; Escape &, <, and >.
(aref table ?&) "&"
;;(aref table ?<) "<"
;;(aref table ?>) ">"
;; Not escaping '"' buys us a measurable speedup. It's only
;; necessary to quote it for strings used in attribute values,
;; which htmlize doesn't typically do.
;(aref table ?\") """
)
table))
Note that both are hacks which simply don't escape the HTML tag delimiters themselves. If syntax highlighting applies to any characters it will break the resulting HTML link by inserting <span>'s.

How to remove the brackets from this text

So I have been given the task by my tutor to make a small function that returns a description of your zodiac sign but I"m having problems with the final output of the sign description, the output is still in brackets and I don't know how to get them out.
(defparameter *month-signs* '((January Capricorn Aquarius 19 nil)
(February Aquarius Pisces 18 nil)
....)))
(defparameter *sign-traits* '((Capricorn (You are a goat be happy!))
....)))
(defun cookie (month-signs sign-traits)
(format t "Please input your month of birth followed by return, and then your day of birth.
e.g january <ret> 22 <ret> Thank You. ~%")
(let* ((month (read t));reads in month of birth
(day (read t));reads in day of birth
(month-assoc (assoc month month-signs)))
(cond
((>=(cadddr month-assoc)day);checks if your sign is the first part of the month
(format t "You are ~D~%" (cadr month-assoc));if it is, prints your sign and signs description
(format t "Your sign description is: ~D~%" (cadr(assoc(cadr month-assoc) sign-traits))))
((< (cadddr month-assoc)22);same as above but for the other sign
(format t "You are ~D~%" (caddr month-assoc))
(format t "Your sign description is: ~D~%" (cadr(assoc(caddr month-assoc) sign-traits)))))))
It all works dandy except this bit "(cadr(assoc(caddr month-assoc) sign-traits)" which returns what I want but in brackets and all caps.
CL-USER> (cookie *month-signs* *sign-traits*)
Please input your month of birth followed by return, and then your day of birth.
e.g january <ret> 22 <ret> Thank You.
january
12
You are CAPRICORN
Your sign description is: (YOU ARE A GOAT BE HAPPY)
I'm really struggling to work out what I need to get rid of the (YOU ARE A GOAT BE HAPPY) on the last bit, I'd like it to just print "Your sign description is: You are a goat be happy." it's probably something obvious that I've missed :\
One more thing the .... are just for your sake as the variable is large and would take up a large amount of page space, I slimmed it down as they are all laid out the same way.
In the list '(January Capricorn Aquarius 19 nil), the first three elements are symbols, which are printed out by the REPL in UPPERCASE. Symbols are different from strings. When you quote a list like this, the literals are treated as symbols, not strings.
Similarly, '(You are a goat be happy!) is a list of six symbols. It gets printed out as a list (enclosed in parentheses) of uppercase symbols.
If you replace the symbols and lists with strings:
(defparameter *month-signs* '((January "Capricorn" "Aquarius" 19 nil)...
(defparameter *sign-traits* '((Capricorn "You are a goat be happy!")...
you should get the output you want.
read takes the input as a symbol, so you want to leave the association key (January) as a symbol.
Printing a list with symbols
Common Lisp can print symbols in many ways. The function WRITE gives a basic interface to the printer. We don't want to print string quotes and we want to control how a word gets capitalized.
(defun write-sentence (sentence
&aux ; local variable bindings
(*print-escape* nil) ; no escaping
(*print-readably* nil)) ; we don't print for the reader
(write (first sentence) :case :capitalize)
(dolist (word (rest sentence))
(write " ")
(write word :case :downcase))
(write ".")
(values)) ; return no values
CL-USER 94 > (write-sentence '(YOU ARE A GOAT BE HAPPY))
You are a goat be happy.
PRINC
The standard function to print in a human readable form is PRINC. It does not provide options, other than to optionally specify the output stream. Those need to be bound in the printer control variables. Here we need to tell PRINC which case to use:
(defun write-sentence (sentence)
(let ((*print-case* :capitalize))
(princ (first sentence)))
(dolist (word (rest sentence))
(princ " ")
(let ((*print-case* :downcase))
(princ word)))
(princ ".")
(values))
Format
Similar functionality can be written in a more compact way using the function FORMAT, which includes features to iterate over lists. This example is left as an exercise to decipher:
CL-USER 115 > (format t
"~{~#(~A~)~#{ ~(~A~)~}~}."
'(YOU ARE A GOAT BE HAPPY))
You are a goat be happy.
Case in symbols
It's also possible to specify symbols with case. They need to be escaped in the source code:
|This is a single symbol in Common Lisp!|

How to print blank character in the Lisp format function?

I hope there's someone to help me with this, because I can't find any useful answer, and I'm new with Lisp.
What I'm trying to do is to test a value of one element and to print something if its 1, otherwise to print blank character.
This works when all of the list arguments have the value 1:
(defun print-lst (list)
(format t "~%~a ~a ~a~%"
(if (= (nth 0 list) '1)
'¦)
(if (= (nth 1 list) '1)
'P)
(if (= (nth 2 list) '1)
'¦)))
so the output is ¦ P ¦. But, if the second element in list is 0, it prints NIL on that place ¦ NIL ¦ and I want it to print a space instead ¦ ¦(not just to skip that character¦¦, it is important to there is a blank character in that position in output line if the tested value is not 1).
Is there any way to return a blank character if the condition (if (= (nth 1 list) '1) 'P) is not fulfilled or is there any other way to perform this?
I hope I explained that nicely. Thank you.
If you want to make full use of the power of format, you can use a combination of format conditionals and format GOTO.
Like this:
[1]> (format nil "~#{~:[<nothing>~;~:*~A~]~^ ~}" 1 2 nil 4 nil 6)
"1 2 <nothing> 4 <nothing> 6"
In your case, this should work:
(format t "~&~#{~:[ ~;~:*~A~]~^ ~}"
...)
This works by doing the following:
~& inserts a newline unless we're already at the beginning of a line.
~#{...~} processes the arguments iteratively.
~:[...~;...~] chooses between the nil and non-nil case.
~:* unconsumes the argument that was consumed by ~:[...~].
~A outputs the item being processed.
~^ escapes from the loop on the last iteration (so as not to output an excessive space after the last item).
If takes three arguments: condition, then-form, else-form; the else-form is optional. Besides, I would use literal character syntax for literal characters.
(if (= (nth 0 list) 1)
#\P
#\Space)
Documentation:
Special form if
Character syntax
Character names
Counter question:
Do you really need output values that are identifiers ('something) or would also string literals work ("something")?
If the first is true: I suppose it is not possible to use space as an identifier.
If the second is true: use "|", "P" and " " as output values
Format is a beast waiting to devour the unwary.
That said, it looks like you may want to use some of its higher level directives here. Check out the Formatted Output section of the Lisp Hyperspec, and the format chapter of PCL (specifically, look at the section that deals with conditional formatting).

Lisp code explanation

I'm porting some code from lisp, but I got stuck at this part (apparently that's for mit-scheme)
(define (end-of-sentence? word)
(and (or (char-exist-last? word '#\.)
(char-exist-last? word '#\!)
(char-exist-last? word '#\?))
(not (initial? word))
(or (eq? (peek-char) '#\Space) ;;peek- so test for linefeed isn't affected
(eq? (peek-char) '#\n) ;;note- test for space isn't neccessary
(eq? (read-char) '#\t))))
;;counts the number of sentences in a given file and
;;stops at the given number
;;returns true once at that position, returns error if eof
(define (goto-sentence? file stop)
(define (c-g-iter num)
(cond ((= num stop)
#t)
((end-of-sentence?)
(c-g-iter (+ num 1)))
((not (char-ready?))
(error "EOF reached, number to large in goto-sentence?: " stop))
(else
(c-g-iter num))))
(begin
(open-path file)
(c-g-iter 1)))
Of course I could just skip that and implement what the comments say it does, but just wanted to be sure there's no magic happening in the background. So... how does this function even work -- where is it actually reading the characters? Is it as ugly as I think and does it consume the characters as a side-effect in the last check in end-of-sentence?? Or does the char-ready? actually read something?
But then again - what does (end-of-sentence?) (c-g-iter (+ num 1)) mean, as I don't expect c-g-iter to return a word.
I'm no scheme programmer, but it appears that characters are being consumed in read-char source
end-of-sentence? on the other hand appears to be getting called without a parameter, even though it is declared to take one. I assume that the functions it calls in turn are tolerant to whatever the system provides for unspecified parameters (nil?)
The pair (end-of-sentence?) (c-g-iter (+ num 1)) is a parameter to cond, which you can think like a switch or concise if/else; the first part is a test (end-of-sentence?), and the second is what to execute if true (c-g-iter (+ num 1))
Just adding my voice to the chorus; maybe I can provide some insight.
Some functions that are in these functions are not standard mit-sheme, such as char-exist-last? and initial?.(1) So I can't be sure what they do.
That being said, I think that end-of-sentence? takes in a string (word, so it should be a word) and returns true if its last character is a '!', '? or '.', and the next character after the word is a space, newline or tab character. Also, looking at intial, it probably can't be the first word in the sentence ('A.', for example, shouldn't return true, but 'A dog.' should.)
read-char does indeed 'consume characters' - "Returns the next character available from input-port, updating input-port to point to the following character." (Googled 'read-char mit scheme' to get MIT-Scheme input procedures.)
Per the same source char-ready? works like so: "Returns #t if a character is ready on input-port and returns #f otherwise."
Hope this is at least someone enlightening!
(1) MIT-Scheme Reference