sbcl: encoding and decoding characters without actual I/O - encoding

In sbcl, when encoding a string using, say, :utf-8, is there a way to encode it to a byte vector without doing actual I/O, similar to clisp's
(EXT:CONVERT-STRING-TO-BYTES string encoding &KEY :START :END)
and also decode with something like clisp's
(EXT:CONVERT-STRING-FROM-BYTES vector encoding &KEY :START :END)
I could crudely approximate this by writing the data to a file with the desired encoding and then rereading it using :iso-8859-1, but that seems to be a dorky longcut.

SBCL has the functions SB-EXT:STRING-TO-OCTETS and SB-EXT:OCTETS-TO-STRING for this.
CL-USER> (sb-ext:string-to-octets "fööbär" :external-format :utf-8)
#(102 195 182 195 182 98 195 164 114)
CL-USER> (sb-ext:string-to-octets "fööbär" :external-format :iso-8859-1)
#(102 246 246 98 228 114)
CL-USER> (sb-ext:octets-to-string ** :external-format :utf-8)
"fööbär"
CL-USER> (sb-ext:octets-to-string ** :external-format :iso-8859-1)
"fööbär"

For portable code, use babel, available from Quicklisp, which has string-to-octets and octets-to-strings like SBCL.

Related

Emacs lisp tutorial: Weird extra output when adding two numbers

I am following the emacs lisp tutorial and have just successfully added two numbers:
(+ 111 234)
I enter this in a random buffer (Markdown mode, now, if that matters, but the same happens in *scratch*), and evaluate it with C-x C-e.
However, the bottom line on Emacs does not simply return 345, but it outputs this line:
345 (#o531 #x159 ?r)
When I submit (+ 2 3), the output is 5 (#o5 #x5 ?\C-e).
What is this extra output? It's not mentioned in the tutorial.
This is the same but in octal (#o...) and hexadecimal (#x...), as well as character syntax.
In Emacs Lisp, non-negative integers and characters are the same type:
(integerp ?d)
==> t
(characterp 123)
==> t
Thus you see ?\C-e for 5 because Ctrl-e has the ASCII code 5.
Your ?r is probably a non-ASCII r which has character code 345 in
your locale.
This is documented in Evaluating Emacs Lisp Expressions.

Emacs truncates some kbd macros during insert-kbd-macro

I am trying to customize emacs for my needs. In particular I want to define a long keyboard macro and save it for later use. However emacs seems to truncate the definition during saving.
Environment: Emacs 24.5-7 on Fedora 23
My attempts showed that emacs uses two different formats for saving depending on the keyboard input. For pure textual form without control characters it just stores a string; no truncation occurred so far. However when a control character, even a newline, is part of the input emacs stores a vector of character symbols?() with only the first 12 elements, the rest is just indicated by ellipsis (...) and cannot be used later.
I use C-x C-( and C-x C-) to define the macro, C-x C-k n to give it a name, and then save it with M-x insert-kbd-macro:
13 printable characters:
abcdefghijklm
(fset 'str13
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("abcdefghijklm" 0 "%d")) arg)))
11 printable characters with newline:
abcdefghijk
(fset 'str11nl
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([97 98 99 100 101 102 103 104 105 106 107 return] 0 "%d")) arg)))
12 printable characters with newline:
abcdefghijkl
(fset 'str12nl
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote (97 98 99 100 101 102 103 104 105 106 107 108 ...] 0 "%d")) arg)))
When I load this macro again, e.g. with M-x eval-region and M-x str12nl, only the 12 printable characters are inserted, then an error occurs:
After 0 kbd macro iterations: Keyboard macro terminated by a command ringing the bell
Is there a method to have the full definition saved?
,

How do I set emacs to use 3 spaces instead of tabs in verilog mode?

I am pretty new to emacs (using version 23.3) and I wanted to set the default tab key to insert 3 spaces instead of a tab character in verilog mode. I did find a number of posts regarding this in stack overflow. Some of them are: -
How To Force spaces instead of tabs regardless of major mode
Why might my Emacs use spaces instead of tabs?
Emacs global configuration of tabs
But they do not seem to work in verilog mode. This is how my .emacs file looks like
(custom-set-variables
'(tab-stop-list ('(3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120)))
'(verilog-case-indent 3)
'(verilog-indent-level-directive 0)
'(verilog-indent-level 3)
'(verilog-tab-always-indent nil))
(custom-set-faces
)
(add-hook 'after-change-major-mode-hook
'(lambda ()
(setq-default indent-tabs-mode nil)
(setq tab-width 3)))
(setq-default indent-tabs-mode nil)
(setq-default tab-width 3)
(setq-default standard-indent 3)
If I try to edit a text file, the setup works perfectly and inserts 3 spaces instead of a tab. However it still inserts a tab character when I try to edit a verilog file (.v). I can select the entire text and do M-x untabify to get the required result but is there another direct solution?
In the hook you should use setq instead of setq-default, so you need to rewrite your hook to something like:
(defun my-verilog-hook ()
(setq indent-tabs-mode nil)
(setq tab-width 3))
(add-hook 'verilog-mode-hook 'my-verilog-hook)
P.S. it's better to use dedicated functions in hooks, as it's easier to change them, and you can also remove them from hooks

Emacs indentation in Asm mode

I am looking for the equivalent of c-indent-level and ruby-indent-level, for asm-mode. That is, I want to force the indentation to 4 spaces, and I want them to be replaced with blanks.
What I've seen tells me it does not exist for asm-mode. Could someone please tell me this is wrong?
I tried this also: Set 4 Space Indent in Emacs in Text Mode , to no av.
I have tried:
(setq tab-width 4)
(setq indent-line-function 'insert-tab)
(setq asm-indent-level 4)
This works however:
(custom-set-variables
'(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))
But I wonder if there is a way to define that for asm-mode only. What if I wanted to keep the default tab behaviour for other modes?
asm-mode uses the function tab-to-tab-stop for indentation, that's why tab-stop-list is working. As far I know there is nothing more that you can do. You might consider using some of the "more advanced" asm modes such as - gas-mode or asm86-mode.
Emacs defines hooks for every (?) major mode. If you do H-m in assembly file, you can see at the end of text section that Assembler mode hook is called `asm-mode-hook'. So you can add your code to run when assembler mode is selected for a buffer like this:
(add-hook 'asm-mode-hook (lambda()
(setq tab-width 4)
(setq indent-line-function 'insert-tab)
(setq asm-indent-level 4)))
Note, tab-width and indent-line-function are already buffer local variables, so setting them changes their value only for the current buffer. This is probably what you want. If you setq some other variables, you may want to make them buffer local using (make-variable-buffer-local VARIABLE) function.

Emacs: getting readable keyboard-macros

When using insert-kbd-macro to save a named keyboard macro I get "unreadable" Lisp code like
(fset 'ppsql
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([134217788 134217765 44 return 44 17 10 return 33 134217765 102 102 backspace 114 111 109 return 17 10 102 111 109 backspace backspace 114 111 return 33] 0 "%d")) arg)))
I'd rather have something like the following:
(fset 'move-line-down
[?\C-a ?\C-k delete down ?\C-y return up])
IIRC I used the same method to record, name, and insert both keyboard macros: F3, F4, name-last-kbd-macro.
Is it possible to get the first macro in a readable format?
The keyboard macro functionality in Emacs stands of two modes: macros and kmacros. The former returns the macro in a way you like—the symbol form—, the latter provides the lambda form. So that, if you call name-last-kbd-macro you get a symbol form, if you call kmacro-name-last-macro, you get a lambda form.
Thanks for that!
So the naming of the macro determines the format when inserting?
I've conducted some more experiments and noticed that M-x insert-kbd-macro RET RET would give me the "symbol-form".
Whereas M-x insert-kbd-macro RET pp2sql RET gives the "labmda-form" (after naming with name-last-kbd-macro).
Now I realize that I've all the way used name-last-kbd-macro in my earlier experiments...?
I've never seen the first form. The second form is what I'm used to. Did you try re-recording the first macro to see what happens if you're sure you record, then name, then insert?
The other thing to try is "C-X (", which invokes kmacro-start-macro and "C-X )" kmacro-end-macro, rather than F3/F4,which are doing something extraneous about keeping a counter. Maybe the simpler keyboard macro command will work more straightforwardly.