How can I htmlfontify an Emacs buffer with composed characters preserved? - emacs

How can I htmlfontify a code buffer in Emacs without losing the composed characters?
I have several modes that display certain ASCII sequences as unicode using compose-region. For example, I might write something like:
foo :: Num a => [a] -> a
foo = foldl (+) 0 . map (\ x -> x + f x - 10)
and the editor displays (without changing the buffer's actual contents):
foo ∷ Num a ⇒ [a] → a
foo = foldl (+) 0 ∘ map (λ x → x + f x - 10)
This is essentially another form of syntax highlighting, except with special characters instead of colors.
However, when I use M-x htmlfontify-buffer on a buffer that looks like my second example, all this information is lost and the html output looks like the first version. How can I avoid losing the character transformations?

I don't think htmlfontify supports this feature yet. But if you M-x report-emacs-bug to request the feature, you might find someone who can implement it fairly quickly (then again, maybe not).

Related

wrap s-expression in slime (emacs)

Is there a short way to wrap an s-expression using slime?
Suppose i have finished some piece of code and I realize I need a variable (e.g. for efficiency reasons) and therefore want to wrap it with let or want to make it tail recursive and need to wrap it with labels, what is the fastest way to do this? Is there a shortcut?
IntelliJ (a Java IDE) allows things like:
x > 3.if + TAB
yielding
if (x > 3) {
}
So are there any "sexp-wrapping" shortcuts (postfix or prefix) in slime/emacs?
The bar represents the cursor.
With paredit
There is a wrap-round command, bound to M-(. But when adding context to an expression, I generally do as follows:
|code
Open the parenthesis (the closing one is added automatically):
(|)code
... type ...
(let |)code
Call paredit-forward-slurp-sexp (C-right)
(let | code)
See also the The Animated Guide to Paredit.
Without paredit
|code
Call mark-sexp (C-M-Space), then kill the region; type and yank (paste) the text where you need it.
See also Expressions with Balanced Parentheses (e.g. forward-sexp), available in Emacs.

M+% replacing value AND also a kill to end of line (C+k)

I'd like to search and replace, say :: and then replace all instances with a single : and a C+k
I had tried M+% then :: and lastly :C+qC+k but it just gave me ^Kactually displaying in text and didn't kill the line.
Thoughts?
EDIT #1:
Just for completeness sake, the data looks like this:
here:: stuf i dont want
more:: oth crud
etc:: I am wicked
I find it hard to understand what you're actually trying to do, but that's probably just me.
1) If what you have is the following:
foo
:: bar
baz
and what you want is this:
foo
:
baz
then you can use regular expression search/replace like this: M-C-% : : . * RET : RET. This calls the function query-replace-regexp to which you pass double colon followed by .* to match the rest of the line. You replace whatever matches with a single colon.
(Note that M-C-% might not work if you run Emacs in the console (-nw) since not all terminals can produce the required escape sequence. In that case, simply type M-x query-replace-regexp instead.)
2) If what you have is the following:
foo
::
bar
and what you want is this:
foo
:bar
then you simply need to include the carriage return in your search pattern: M-% : : C-q C-j RET : RET. Note that this does not require regexp search, normal string search is fine for this purpose.
3) If what you have is the following:
foo
:: bar
baz
and what you want is this:
foo
:baz
then you can combine the two techniques above and do: M-C-% : : . * C-q C-j RET : RET. Here, we include the carriage return in our regular expression search pattern.
Create a keyboard macro where you first I-search for ::, remove the last : and then hit C-k. Execute that keyboard macro as long as it matches. This is how I would do it :-)

How can I input Greek symbols like \beta, \lambda in emacs?

How can I input Greek symbols using the TeX descriptions (like \beta, \alpha, \lambda, etc) in emacs?
Use
C-x RET C-\ TeX RET
to get TeX entry mode. Then you can use many of the standard sequences from TeX to enter things.
\alpha α
\Gamma Γ
as well as the whole gamut of mathematical symbols
\rightarrow →
\Leftarrow ⇐
\oplus ⊕
\int ∫
x_3 x₃
M^-^1 M⁻¹
v^x vˣ
I tend to use the RFC 1345 input mode for this.
C-x RET C-\ "rfc1345" RET
You can then turn the input mode on and off with C-\.
According to RFC 1345, Greek characters are accessed by appending the * character. Special symbols in general start with &. So you can type &a* and get α, &l* gets λ. I use RFC 1345 because it also gives me access to all sorts of other symbols, like &TE for ∃ "there exists" or &FA for ∀ "for all".
You could also use the Greek input mode, which is easier to use but doesn't give you extra symbols.
C-x RET C-\ "greek" RET
One of the advantages of these methods is that they work the same way across all platforms — as long as you're using Emacs.
i always input `λ' by the following punch:
C-x 8 RET 3bb
You can also learn some unicode and use M-x ucs-insert, e.g. M-x ucs-insert 3BB gives you a beautiful lambda.
There is also the greek-babel input method which is selected (similarly to other input methods) with C-x RET C-\ greek-babel.
In this mode, the regular alphabet keys are mapped to greek characters as shown in this little table:
Αα Ββ Γγ Δδ Εε Ζζ Ηη Θθ Ιι Κκ Λλ Μμ Νν Ξξ Οο Ππ Ρρ Σσς Ττ Υυ Φφ Χχ Ψψ Ωω
Aa Bb Gg Dd Ee Zz Hh Jj Ii Kk Ll Mm Nn Xx Oo Pp Rr Ssc Tt Uu Ff Qq Yy Ww
plus some for entering sampi, digamma, stigma, koppa and various diacriticals. For a full list of the mappings done by greek-babel use C-h I greek-babel.
In ordinary usage the only somewhat weird mappings to watch out for are Θ/θ, ς, Υ/υ, Ψ/ψ and Ω/ω.
It is named so because the LaTeX package babel when loaded with the greek option enables one to type greek text with regular latin latters using the above mapping.
This input mapping is especially useful if you're writing the occasional Greek word in a (La)TeX document that you want to keep fully in UTF-8 without any ugly hacks like writing bibloc in your source instead of actual Greek βιβλος.
<Rant>
As a side note, things like C-\ are really annoying with a Danish keyboard because \ = AltGr + <, so to enter C-\ you need to hold down both Ctrl keys plus AltGr. If you hold just one Ctrl key plus AltGr plus < you just get a backslash.
</Rant>
I have Ubuntu set up so that I can type in Greek after pressing Shift+CapsLock — λικε τηισ — and then press Shift+CapsLock again to get back to Latin letters. Are you using Windows, MacOS, what?
Edit: You're using Ubuntu too, so: go to Start menu → System → Preferences → Keyboard → Layouts. [Add...] a layout for Greece. Select a key combination you like under [Options...] → Key(s) to switch between layouts. Personally I don't have "Separate layout for each window" set, but YMMV. Finally, I clicked [Apply System-Wide...].
(I'm using Ubuntu lucid 10.04 LTS — if you're using a newer version, the steps might be slightly different.)
Depending on your goal, e.g. which programming language you want to edit, you might want to consider using the input mode available in agda2-mode; it provides a more programming oriented alternative to TeX (but very similar);
Use Agda's input method in other emacs mode?
http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.UnicodeInput
I'm personally using it with my Haskell buffers instead of the TeX input method or the automagic but limited Haskell unicode input method.
Examples:
typing \:: yields ∷
typing \-> yields →
typing \br yields ℝ
Installing agda2-mode means using your package manager or Cabal to install Agda (e.g. brew install agda on OS X), then $ agda-mode compile and then:
(load-file (let ((coding-system-for-read 'utf-8))
(shell-command-to-string "agda-mode locate")))
Optionally, to auto-enable the agda input mode for e.g. Haskell buffers:
(require 'agda-input)
(add-hook 'haskell-mode-hook
(lambda () (set-input-method "Agda")))
if you have an Apple, there is a very easy way to access all of the characters available to the "Character Viewer".
In Sys Prefs -> keyboard -> input sources, select show input menu in menu bar.
Bring up the character viewer and then make favorites of the N characters you need to access. Then, hit the small box to the right of char viewers search bar. The char viewer disappears, and what's left is a small window with your chosen char set. Position your cursor in your editor, click on the desired char in the favs.
You can copy paste them from the character map to the editor.

emacs/elisp: What is the hash (pound, number sign, octothorp) symbol used for?

What does this do?
(add-hook 'compilation-mode-hook #'my-setup-compile-mode)
...and is it different than
(add-hook 'compilation-mode-hook 'my-setup-compile-mode)
There is no difference:
(eq 'my-add #'my-add)
yields t
The # can be used in front of a lambda expression indicating to the byte-compiler that the following expression can be byte compiled, see the docs for Anonymous Functions. But there's nothing to compile in the case of a symbol.
In general, it is used in the printed representation along with the left angle bracket (<) to indicate that the object printed is a description (but cannot be read). For example:
#<buffer foo.txt>
It is also used in constructs by the reader to represent circular structures. See the docs for Read Syntax for Circular Objects.
And then you have its use for denoting the base for integers, e.g. #x2c -> 44.
Plus more I'm sure.
The should-be-comprehensive list can be found at the top of the Emacs lisp reference index.
Edit: Or even more conveniently, from within Emacs itself:
M-x info RET (open the info browser)
d m elisp RET (open the elisp manual)
I # RET (list the entries for # in the index)
I found this question while searching for what the hash meant in something I found while hacking mode-line-format:
#("-%-" 0 3
(help-echo "Display as tooltip when mouse hovers or with display-local-help."))
which is a format used for text properties in strings where:
"-%-", text to be propertized: one dash and a %-construct that results in "dashes sufficient to fill the remainder of the mode line", resulting in the famous Emacs ------.
0, the first character upon which the text properties apply.
3, the last character upon which the text properties apply, i.e. the entire "-%-".
(help-echo "..."), a property and a string as its argument.
This can be created with the propertize function:
(propertize "Hover over me!" 'help-echo '"congratulations!")
would be the same as #("Hover over me!" 0 14 (help-echo "Congratulations!")):
If you're using font lock mode, using the buffer-substring command might produce something like this:
(buffer-substring 1 28) ; First 27 characters in the current buffer
⇒ #(";; This buffer is for notes"
0 3
(fontified t face font-lock-comment-delimiter-face)
3 27
(fontified t face font-lock-comment-face))
So you could create something like:

How do you type lisp efficiently, with so many parentheses? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I try to keep my fingers on home row as much as possible.
Typing all the parentheses makes me move away from there a fair bit.
I use Emacs; the parentheses themselves are no issue, I'm comfortable with them. And I don't like modes that type them for me automatically.
I've thought about remapping the square brackets to parentheses and vice versa. Is this a good idea? What does everyone else do?
I would personally recommend the lethal combo of Emacs, SLIME & paredit.el
Paredit allows you to pseudo-semantically edit the LISP code at sexp level, and that makes the parentheses disappear almost completely. You can move around sexps as if they are simple words and even mutate them with just a couple of key-strokes. There is also a minor mode called Redshank which works in conjunction with Paredit and that provides more power.
Consider simple these examples (| is the position of the cursor):
(foo bar (baz| quux) zot) + C-( => (foo (bar baz| quux) zot)
(foo (bar |baz) quux zot) + C-) => (foo (bar |baz quux) zot)
(foo (bar baz |quux) zot) + C-{ => (foo bar (baz |quux) zot)
(foo (bar |baz quux) zot) + C-} => (foo (bar |baz) quux zot)
I have done some serious Common Lisp programming in my time, and paredit has proved to be invaluable to me. I can't even think of writing non-trivial LISP code without my toolbox. And after all that's the way it should be; you should never have to count or match your parentheses... once you master Paredit, the parentheses will just disappear from in front of your eyes.
Remapping the [] to () will help, but you will stop caring much after you start using Paredit.
Feel free to use the LISP specific portions from my dot-emacs.
With many non-US keyboard layouts, typing square brackets or braces is even more cumbersome than typing parentheses, anyway, which makes programming in most languages very strainful, so consider yourself lucky. ;)
As for me, I use a programmer-friendly non-standard keyboard layout that lets me type parentheses via [Super]-j and [Super]-k.
I have foot pedals. LeftFoot = open paren, RightFoot = close paren.
Well, I don't, but I don't use Lisp. It doesn't seem like a bad idea, though.
Could you imagine a variation on Lisp that used indentation instead of parens? (taking a page from the Python spec)
I take my fingers off the home keys....
I tried remapping in Emacs, but it creates new problems: say you're editing in a terminal window through ssh and you paste a snippet into the window; then parens and brackets get swapped in your pasting, not just your typing. If you try this, remap at a lower level in your system, like xmodmap.
(Of course, the obvious other problem is using other computers without your remapping. That was a nuisance too, though bearable.)
"... so many parenethesis"
The first thing I did was bind the '(' key to the sequence '('+')'+right(), so my parenthesis auto balance, leaving half as many left to type when writing new code.
You also want a convenient way to navigate out one paren -- bind C-] to the sequence search(')')+right(). Authoring becomes shorter now, as you don't need to take hands off the home position -- just type C-] every time you complete an S-expr.
Next thing I did was bind a key to a subroutine that pushes an existing item onto the current list ... so if // is the cursor position, then this command will transform:
(if (< //) (+ x 1)
(x)
(y))
to
(if (< (+ x 1) //)
(x)
(y))
Effectively pushing one item from the right into the current list -- very useful for editing existing code. The sequence '(', '<', C-S-], Space, '2' adds "compare less than 2" to an existing expression. Combined with C-], this lets you build new expressions very quickly from existing ones.
#jamesnvc,
I didn't think about binding () to [] keys... I'll have to try that, thx!
I remapped [] to () with xmodmap and like it. It was a bit weird getting used to writing code in languages that use [], but like any change, you get used to it. Having unshifted parens in Lisp is nicer than not having unshifted brackets in other languages, so it works out.
Anyway, here is the necessary xmodmap incantation for my US keyboard:
!! swap () and []
keycode 18 = 9 bracketleft
keycode 19 = 0 bracketright
keycode 34 = parenleft braceleft
keycode 35 = parenright braceright
I have to take my fingers off the home row to reach all the other shift-number operators, so I never thought about it much.
And once you type a left-parens, electric-parens give you the right.
If you use the parentheses more than the square brackets, by all means, remap away. I don't see how it could pose any more problems than, say, a lefty swapping her mouse buttons.
When I'm writing code, I generally spend much more time thinking and reading my code, than I do typing it. I've tried a couple of times in the past to switch to the Dvorak keyboard layout, but I lack obvious motivation because I can type much faster than I can think. Programming language syntax is a similar issue - as long as I can type code without leaving the keyboard (ie. using the mouse would be bad), I'm happy.
DrScheme has the keystrokes for parens and square braces flipped by default. It also has a feature where it magically guesses which one of the two you meant, so you rarely reach for shift-9.
Quack has a similar feature to that of DrScheme.
DivaScheme (my editor), is something completely different. It edits at the sexp level, so that the parens are no longer in the way.
Mostly, I just type them, but occasionally, I use M-( and M-) (especially when I am adding a LET binding "late in the stage"), to enclose the relevant nnumber of expressions.
I also changed my (dvorak) keyboard layout (via xmodmap) to switch the brackets ("[]") and parens, in conjunction with paredit-mode (which does indeed take some getting used to).
I use paredit and pair-mode packages but, for fast parenthesis typing I use electric-dot-and-dash to replace a double period in a () on a 5 ms delay (if I type slowly I get two dots then). It's a wonderful package (I hacked a bit for my personal preference; as I type with Dvorak keyboard, I replaced the dash key by a slash (// is not so common in lisp)).
To avoid the mess in parens, I add a package named 'highlight-parentheses to my tool set, and for maximum efficiency on sexp grabbings or text navigation in general, I also use vimpulse (as I am a Vim addict).
Rebind capslock to "(" and have the editor autoinsert ")" for you.
(This also helps for other languages with a lot of brackets, for instance HTML...)
I use Vim with vim-sexp and vim-sexp-mappings-for-regular-people then have mapped <leader>u to put me in normal mode inside of a new (.
map <leader>u i(