For example, one line of code in my function
(message "char %c:%d" character count)
will print the counts for each character. For nonprintable chars, such as newline and tab, I want the output looks like:
\n:4
\t:6
instead of printing a newline and tab literally. how can I do that?
You can achieve some of this by let-binding certain variables before printing.
`print-escape-newlines' is a variable defined in `C source code'.
Its value is nil
Documentation:
Non-nil means print newlines in strings as `\n'.
Also print formfeeds as `\f'.
There's also:
print-escape-nonascii
Non-nil means print unibyte non-ASCII chars in strings as \OOO.
print-escape-multibyte
Non-nil means print multibyte characters in strings as \xXXXX.
These all work with prin1, so you can use the %S code in format. e.g.:
(let ((print-escape-newlines t))
(format "%S" "new\nline"))
As suggested by #wvxvw
(defun escaped-print (c)
(if (and (< c ?z)
(> c ?A))
(string c)
(substring (shell-command-to-string (format "printf \"%%q\" \"%s\"" (string c)))
2 -1)))
The substring part is to cut out extra stuff from printf's output. I don't have a great deal of knowledge about this command, so it might not be flawless.
There may be some code somewhere in emacs that can do this for you, but one way would be to write a function that converts the special characters to a string:
(defun print-char(c)
(case c
(?\n "\\n")
(?\t "\\t")
(t (string c))))
Note that you need to use string format rather than character because you're actually writing multiple characters for each special character.
Related
In python you can write this r"c:\data"
How do I the same in lisp? ie I want a reader macro that has the same functionality so I no longer need to escape backslashes in windows paths. I do not want to use forward slashes as plenty of commands in windows dont understand them.
For instance
(defun read-literal-string (stream delimiter arg)
(declare (ignore arg))
(loop for char = (read-char stream nil stream)
when (eq char stream)
do (error "hit end of stream")
until (char= char delimiter)
collect char into chars
finally (return (coerce chars 'string))))
(set-dispatch-macro-character #\# #\" #'read-literal-string)
And now
> #"fo\o"
"fo\\o"
> #"foo\"
"foo\\"
obviously a real version would be more carefully written
I was very surprised not to be able to find this in the elisp manual or SO. I just want the equivalent of many languages' chr() and ord() or similar: convert between actual characters and their (unicode) code point values.
Emacs Lisp: getting ascii value of character explains that to elisp, a char just is its code-point. But what if I need the representation of that char~int as a series of ASCII decimal digits?
For example, if I wanted to generate in a buffer, a readable table showing the equivalences?
Thanks!
As you've already noted, characters are integers.
(eq ?A 65)
For example, if I wanted to generate in a buffer
Either of the following inserts the character A into the buffer:
(insert ?A)
(insert 65)
If you need to deal with strings, characters can be converted to strings:
(char-to-string ?A)
(char-to-string 65)
(format "%c" 65)
"A"
vs
(number-to-string 65)
(format "%d" 65)
"65"
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.
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).
Emacs Lisp has replace-string but has no replace-char. I want to replace "typographic" curly quotes (Emacs code for this character is hexadecimal 53979) with regular ASCII quotes, and I can do so with:
(replace-string (make-string 1 ?\x53979) "'")
I think it would be better with replace-char.
What is the best way to do this?
This is the way I replace characters in elisp:
(subst-char-in-string ?' ?’ "John's")
gives:
"John’s"
Note that this function doesn't accept characters as string. The first and second argument must be a literal character (either using the ? notation or string-to-char).
Also note that this function can be destructive if the optional inplace argument is non-nil.
Why not just use
(replace-string "\x53979" "'")
or
(while (search-forward "\x53979" nil t)
(replace-match "'" nil t))
as recommended in the documentation for replace-string?
which would certainly be better with replace-char. Any way to improve my code?
Is it actually slow to the point where it matters? My elisp is usually ridiculously inefficient and I never notice. (I only use it for editor tools though, YMMV if you're building the next MS live search with it.)
Also, reading the docs:
This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
(while (search-forward "’" nil t)
(replace-match "'" nil t))
This answer is probably GPL licensed now.
What about this
(defun my-replace-smart-quotes (beg end)
"replaces ’ (the curly typographical quote, unicode hexa 2019) to ' (ordinary ascii quote)."
(interactive "r")
(save-excursion
(format-replace-strings '(("\x2019" . "'")) nil beg end)))
Once you have that in your dotemacs, you can paste elisp example codes (from blogs and etc) to your scratch buffer and then immediately press C-M-\ (to indent it properly) and then M-x my-replace-smart-quotes (to fix smart quotes) and finally C-x C-e (to run it).
I find that the curly quote is always hexa 2019, are you sure it's 53979 in your case? You can check characters in buffer with C-u C-x =.
I think you can write "’" in place of "\x2019" in the definition of my-replace-smart-quotes and be fine. It's just to be on the safe side.