Emacs mode/method for logic symbol placement in text? - emacs

I would like to put actual logic symbols into my emacs buffers, e.g., the logic symbol "∀" or "∃" or "⇒", directly into my (fundamental) text or .org or whatever buffer. I found xmsi-math-symbols-input.el at ErgoEmacs, but I'm wondering if this is "best practice." Maybe the best practice is to just right Tex/Latex copy, especially if I'm doing org-mode?

You can just use the corresponding Unicode characters normally in Emacs. Bind any that you want to any keys you want. For example:
(global-set-key [f2] "∀")
(global-set-key [f3] "∃")
(global-set-key [f4] "⇒")
To get the string with the char, you can use C-x 8 RET and type the name or code point of the Unicode char. In other words, C-x 8 RET lets you insert any Unicode character.
For example, the Unicode code point for ∀ is 2200. C-x 8 RET 2200 RET inserts a ∀ character.
And the Unicode name of ∀ is FOR ALL. C-x 8 RET for all RET also inserts a ∀ character.
The reason you might want to bind a particular character to a key is for convenience - C-x 8 RET is very general, and generally slow.

At least in org-mode, it is possible to place special symbols in an .org buffer just as their raw latex markup, e.g.:
\forall
becomes the UTF-8
∀
when you do C-c C-x \
... but this isn't a general solution.

The way I do it is with the TeX input method. I.e. use C-u C-\ TeX RET after which typing \forall will insert the ∀ char.
[ Well, in reality , I have TeX set as my default coding-system, so I really only need to hit C-\ to enable this input method. ]

Related

What does Emacs describe-key expect for input?

I'm in Emacs' *scratch* and I try
(describe-key "C-x C-f")
and I get back
"C - x SPC C - f is undefined"
Obviously, it doesn't recognize my C-x C-f shorthand. What does it expect? The documentation
Display documentation of the function invoked by KEY. KEY can be any
kind of a key sequence; it can include keyboard events, mouse events,
and/or menu events. When calling from a program, pass KEY as a string
or a vector.
doesn't elaborate on how that string or vector should be entered. What do I need to give it to work?
You need to specify it as (describe-key "\C-x\C-f") or (describe-key (kbd "C-x C-f")).
if you call C-h f kbd, you'll get:
(kbd KEYS)
Convert KEYS to the internal Emacs key representation. KEYS should be a string in the format returned by commands such as ‘C-h k’ (‘describe-key’).
This is the same format used for saving keyboard macros (see ‘edmacro-mode’).
You need to specify the keys exactly. "C-x C-f" is a string consisting of the capital letter C, a dash, a lower case x, a space etc.: it's a description of what you press.
What you really press is [24 6] a vector of two chars (chars are represented as integers internally and 24 and 6 are the integers representing Control-x and Control-f resp). Another way to describe the vector of these two chars is as a string consisting of two chars: "\C-x\C-f" is one way, as Alex Ott points out in his answer; equivalently "\x18\x6" uses the hexadecimal values of 24 and 6 resp; or "\030\006" uses the octal values.
But we'd rather use the description "C-x C-f", so (as Alex Ott points out in his answer again) kbd translates from the description to the actual sequence of chars:
(kbd "C-x C-f") --> "^X^F"
which is actually two characters: Control-x and Control-f. The lisp printer uses the caret representation to indicate them, so it looks like four characters in between the quotes, but that is just how they appear: if you do the above in your *scratch* buffer, put the cursor on the left quote and press right arrow or C-f on them, you'll see that there are only two characters in between the quotes.
All of this and more is explained in the GNU Emacs Lisp manual, in the Character Type section and its subsections.

how can a person add abbreviated names for unicode character names in Emacs

Using emacs-24.
Some unicode names are quite long. Some characters have more than one name depending on the context. I would like to add some abbreviations/synonyms. How?
This approach is not so bad, but I have problems with shorter names that alias with longer ones, and it is non-standard, i.e. not consistent with the way other names are entered:
(global-set-key (kbd "C-x g all") "∀")
The approach of putting characters on keys has problems in Emacs, partly because the keymap is already overloaded:
(define-key key-translation-map (kbd "C-~") (kbd "¬"))
As a secondary question, I am curious as to why this confuses emacs (give it a try):
(global-set-key (kbd "C-x g neg") "¬")
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
You can easily define a command that inserts a given character (or that chooses from some small set of characters rather than from the entire universe of Unicode characters).
Library ucs-cmds.el can help with this. When you use C-x 8 RET with a negative prefix arg (e.g. C--), it not only inserts the char you choose but it creates a command to insert the char - the command name is the same as the char name. And you can quickly create such commands for whole ranges or other sets of characters (e.g. by matching a regexp). You can of course rename commands to whatever you like, including shorter versions.
But you already know how to bind a key to a keyboard macro that inserts a given character, as you have shown. If it helps to provide a named command for that then ucs-cmds.el can help.
You can also just do that yourself individually, using, for example:
(defun neg (&optional n)
"Insert \"¬\". With prefix arg N, insert N times."
(interactive "p")
(dotimes (ii n) (insert "¬")))
(global-set-key (kbd "C-x g neg") 'neg)
But you apparently are not very interested in dedicated commands that insert particular characters, and you want to be able to use C-x 8 RET but to type an abbreviation for a character name when it prompts you, instead of trying to match the real character name.
For that, Icicles can help. When you use C-x 8 RET you can match the character name or its code point (or the character itself - useful when the char is easy to type and you want to know its name or code point). You can match any combination of these at the same time.
Matching can be substring, regexp, pcompletion or any of several kinds of fuzzy matching, and you can change the matching behavior on the fly. So you can get the effect of the abbreviations you are asking for, provided you abbreviate in a way that corresponds to matching.
As for your question about (global-set-key (kbd "C-x g neg") "¬"): I think it is a bug. Consider reporting it: M-x report-emacs-bug. This is the error that it raises:
After 0 kbd macro iterations: user-error: No M-x tags-search or M-x tags-query-replace in progress
There are several modes around which provide simplified input for symbols needed by math and logic. For example agda2-mode. http://wiki.portal.chalmers.se/agda
OP:
What I would like is to hook the abbreviations into the current emacs method for entering unicode characters by name. (I've been using C-x 8 RET name RET - though wish there was a method to do this in fewer key strokes.)
What the OP is asking for is:
a) To use the emacs function 'insert-char' with its built-in shortcut 'C-x 8 RET', and
b) To use an alias for completion in the interactive minibuffer for 'insert-char' input.
The issue is that the minibuffer for 'insert-char' has its own TAB completion. If you want to insert the greek small letter epsilon (ε) using TAB completion, you have to input a minimum number of keystrokes like this: "greek" TAB "sm" TAB "l" TAB "ep". Even if you have an alias for epsilon in your 'init.el' configuration file like this: '("eps" "GREEK SMALL LETTER EPSILON")', the minibuffer will not automatically recognize it.
You can still use the alias for epsilon you have in your 'init.el' file using a second function 'expand-abbrev'. Using the method described in the OP, you can get an 'ε' by using "C-x 8 RET" (or "M-x insert-char"), entering your alias "eps", then call 'expand-abbrev' ("M-x expand abbrev") and return. This will expand your alias for the 'insert-char' function. (There is also a 'C-x' shortcut for 'M-x expand-abbrev'.)
Like the OP, I prefer this method over (or in addition to) automatic alias replacement. If you have something in your config file like this:
;; a quick way to insert unicode characters by code point or name
(global-set-key [f8] 'insert-char)
;; call 'expand-abbrev', especially in the 'insert-char' input minibuffer
(global-set-key [f9] 'expand-abbrev)
;; abbreviate unicode names
(define-abbrev-table 'global-abbrev-table '(
("ueps" "GREEK SMALL LETTER EPSILON")
("Ueps" "ε")
("ugsl" "GREEK SMALL LETTER ")
("uforall" "FOR ALL")
("Uforall" "∀")
))
;; see .emacs.d/abbrev_defs
;; M-x edit-abbrevs
;; turn on abbrev mode by default
(setq-default abbrev-mode t)
, you have two ways to type an epsilon. You can let emacs replace the alias "Ueps" automatically, or you can use seven keystrokes "[f8]ueps[f9]RET". (Actually there are four ways here.)
As the OP suggests, it is somewhat impractical to have aliases (or key-bindings) for every single special character. That is why it makes sense to use 'insert-char' with 'expand-abbrev'. If you want to insert a less commonly used greek letter like omicron 'ο', for instance, you do not need a special alias; you can expand an alias like 'ugsl' to "GREEK SMALL LETTER ", and enter "omicron" (or "omi" + TAB).

how to enter tilde in emacs with portuguese keyboard?

In every single other program I have ever used in the last 15 years across windows, osx and linux, I enter a tilde by pressing, the tilde key and then space. The Portuguese keyboard has a dedicated key for tilde where it is the primary character (no need for shift), it is used to compose ã and õ by pressing tilde then a or o. In emacs pressing tilde does nothing and posts "dead-tilde is undefined". How can I make emacs write a '~' when I press the '~' key in pt layout ?
Edit:
I think this is a better solution: It should match your experience in other applications where ~o gives õ and ~ followed by a space gives ~.
Tell Emacs you wish to use the portuguese-prefix input method. Interactively, you can do M-x set-input-method RET portuguese-prefix RET. To make this permanent, add something like this to your config file:
(set-input-method 'portuguese-prefix)
Original answer:
self-insert-command doesn't seem to work well with dead keys.
Try this instead:
(defun my-insert-tilde ()
(interactive)
(insert "~"))
(global-set-key (kbd "<dead-tilde>") #'my-insert-tilde)
add
(require 'iso-transl)
to Emacs init file (init.el). With this line tilde+space prints a tilde, and tilde+a prints ã.
This seems to be due to "Emacs and some input method managers (ibus and SCIM) don’t work together".

How to add a ℃ in org-mode which can be correctly exported to latex

I tried sometimes and realized that exporter can recognize $1100^{\circ}C$ and 1000^\circ, but can not recognize 1000^\circC and 1000^\circ C correctly, so which is the best way to add a ℃? I would not like to use $1100^{\circ}C$, because it need to add two whitespaces in both sides.
Why not use an appropriate wysiwyg character here.
A sample org file:
Foo! The temp is 12 °C.
The PDF after C-x C-e l o:
Different extended alphabet symbols can be typed with Emacs' C-x 8 subbindings. For instance:
Key Gives
---------------------
C-x 8 o °
C-x 8 u µ
Be sure to check C-x 8 C-h for some of the mapped symbols.
Check also the input method TeX. It is pretty cool. It translates directly written TeX macros into unicode symbols. C-\ TeX RET and you're set.
If i would insert a Celsius symbol with C-x 8 o , it still would convert it to a non printable or useless character. The only way the Celsius sign works for me is when i insert it in org-mode with \textdegree
Another possibility is to replace the symbol with some latex command. I would recommend the siunitx package which I think yields better results in general. For example, with the code below and \usepackage{siunitx} in the header, you could write 12 °C in the Org document and it becomes \SI{12}{\degreeCelsius} in the tex file.
(defun org-latex-replace-degree (text backend info)
(when (org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string
"\\([0-9\.]*\\)\s?\\(°C\\)" "\\\\SI\{\\1\}\{\\\\degreeCelsius\}" text)))
(add-to-list 'org-export-filter-plain-text-functions 'org-latex-replace-degree)

Text processing to concatenate lines (ie remove end of line char)

I have a list of lines like this:
a+
b+
c+
d+
e+
f+
... you get the idea...
I want to end up with a+b+c+d+e etc
I was trying with emacs but couldn't work out how to do such a thing. anyone any ideas?
One thing that does work is
c-m-% [paste in selected after + on one line to beginning of next row] [nothing]
There must be something to insert for carriage return?
How about simply replacing EOLs by nothing?
M-%C-q C-jRETRET
Explanation:
M-% : query-replace
C-q : quote the following character
C-j : end-of-line character
first RET : validate the search string
second RET : validate the (empty) replacement string
Do you have a buffer with those lines in it? In that case, you could create a simple macro:
F3 ;; record macro
C-e ;; end of line
C-d ;; delete newline
F4 ;; save macro
Then either press F4 repeatedly until you're done, or do C-0 F4 to do it all in one swoop.
Have you tried just `M-q' ? The spacing is different, and it will use several lines if you have many of those thingies, but otherwise, it seems like a funny alternative.
M-x
replace-regexp
RET
C-q C-j
RET
RET