emacs CUA-mode OK in X but not working console - emacs

I have been struggling to get emacs CUA-mode working in the console where it works fine in X (in an xterm using emacs -nw).
The main features I want are shift-movement selection and ^C,^V,^X but any more would be helpful. So far I am using a local keyboard and monitor but ultimate goal is to get all this working over ssh using something like putty.
So far I have been working on getting my terminal to output the same control sequences that emacs expects to see for shift-movement (specifically S-<down> or <S-down>) and this works in the console when shift-select-mode is set in .emacs. So far so good, so I can shift select text from any emacs invocation type: X-windows, terminal ($TERM=linux or $TERM=xterm). This site has been very useful.
Shift-<down> displays as follows using cat -v and emacs happily maps <S-down> to this control sequence.
cat -v (then type shift-down)
^[O2B
Here's the problem, switch on cua-mode and the shift-selection stops working but ^C,^V and ^X starts working !? So I can either have shift-selection or ^C,^V,^X but not both at the same time.
-------
Setup:
emacs 23
Raspberry Pi
Raspian (similar to Debian)

You might like to try a more recent version of Emacs, since cua-mode has been slowly changed to use the same code as shift-select-mode. In Emacs-24.4, cua-mode actually uses shift-select-mode directly.

Related

How are keyboard inputs transmitted through Windows terminal and WSL2

I'm using WSL (WSL2 in fact) for a few days and I'm a bit confuse about keyboard combination redirection and/or interception.
I have to say that my favorite tool to edit text is emacs, that use a lot of keyword combinations.
My stack is this one :
Under windows, I run Windows terminal (WT.exe)
In this terminal I open a tab which (afaik magically) is a shell on a linux subsystem. This subsystem is a Debian
In this shell I run emacs.
The whole thing looks like that :
At this point, when I enter a key combination, it may be passed to emacs, or it may be intercepted/interpreted by any other layer of the stack.
For instance :
ctrl-e is interpreted by emacs : it move the caret to the end of file
ctrl-z is interpreted by wsl2 (afaik) : it pauses emacs as a background job
ctrl-tab is interpreted by windows terminal : it changes tab
alt-f4 is interpreted by windows : it closes windows terminal
And some of them are a mystery for me. For instance ctrl-_ (ctrl underscore), which is very useful on default emacs configuration, is not interpreted by emacs. It is translated at some point as "delete". In this configuration typing "delete" or "ctrl-_" is the very same thing : it removes the first character before the caret.
Very specifically and synthetically stated, my question would be : "How do I configure this stack to make emacs receive ctrl-_ ?"
Of course more generally, I really would like to know, how each layer works and is parameterized. For instance, is there a configuration file for WT and/or WSL2, that says which combination are forwarded, which one are interpreted and which one are ignored.
Additional Data :
If I run WSL2 through WT, but don't start emacs, ctrl-_ is also interpreted as "delete"
If I run another type of "tab" in WT, like cmd.exe or powershell, ctrl-_ is not interpreted as "delete". I don't know if it is interpreted at all
If I run WSL2 without WT, Ctrl-_ is not interpreted at all.
I didn't found any mention of ctrl-_ in WT config shortcuts.
Additional Data :
I tried to run following python program in both stacks : WT/WSL and WT/CMD.EXE
import keyboard
while(True):
c = keyboard.read_key()
print(c)
On WT/CMD.EXE, the output is :
ctrl
_
_
ctrl
Which means it receive the keyboard info.
On WT/WSL2, it fails to run. Apparently "keyboard" is not compatible with WSL2. But this is not the question here.
A useful tool for debugging input on linux is showkey -a. You can use that to help debug what keys the client application is receiving. If you press a key and it doesn't display anything in the output, then there's a good chance it's being intercepted by the Terminal.
There's probably an open issue for this on the Terminal repo. #8458 looks similar.
is there a configuration file for WT and/or WSL2, that says which combination are forwarded, which one are interpreted and which one are ignored.
defaults.json is the file that contains all the keybindings that the Windows Terminal binds by default.
What WSL does is more complicated. IIRC, the inputrc file on linux is used readline (for bash, etc) to specify how certain keys are handled by the shell. But then there are also keys configured in stty that control other parts of the TTY interface. That part I understand less.
alt-f4 is interpreted by windows : it closes windows terminal
FWIW That's actually a application-level keybinding. The Terminal handles that key manually, so that people can optionally disable it. See defaults.json

Emacsclient crashes when evaluating window functions

To not bore anyone here with specifics, whenever I evaluate an expression similar to this one:
emacsclient -t -e '(set-buffer *scratch*)'
the client will flash up on the terminal and crash.
This seems to be happening with all window-changing functions. Is the client not supposed to work like that? Running this in a normal emacs session does not cause this problem.
You're also using -t, but I'm not exactly sure why.
Is your emacs running in another tty session?
Or is your emacs running in windowing mode (e.g. on X Windows)?
If I have emacs running in windowing mode and I run the following command from another xterm window, then everything works exactly as I would expect it to:
emacsclient -c -e '(set-buffer "*scratch*")
Note in particular the -c option, and the fact that the buffer name is a string and so must be enclosed in double-quotes.

How can I write to standard output in Emacs

I am trying to debug an Emacs program performance wise. In particular, I suffer an extremely long startup time (~5' compared to ~1' for a bare Emacs) on a remote connection via WLAN, cellphone etc. In this context, any message written is no help, for the display is not refreshed at all.
What I would like to do is to write onto the "standard output" of the Linux process. I am aware of the --batch mode but this is no help to me because I want to use Emacs interactively.
So how can I write messages out to the Linux-standard output (as opposed to the Emacs standard output)?
You can output to standard error like this:
(print "hello world" #'external-debugging-output)
or
(princ "hello world" #'external-debugging-output)
This can buffer, so be careful.It's not possible to output to standard out at the moment. I'm going to add that, I think!
Start emacs as a daemon (emacs --daemon) and any messages during the start-up sequence will be sent to stdout or stderr, as described by lunaryorn.
Connect to the server with emacsclient
The simplest way to kill the server is M-x kill-emacs RET
For details see C-hig (emacs) Emacs Server RET
Works for me in centos 6.8 (GNU Emacs 23.1.1):
(append-to-file "here I come to save the day\n" nil "/dev/stdout")
Try also using "/dev/tty" in place of "/dev/stdout":
Unclear from question if you intend to redirect "emacs -nw" stdout to a file and monitor that file externally (then use "/dev/stdout"); or are ok with writing to "/dev/tty" thus polluting the self-same tty of the main "emacs -nw" display.
If starting a GUI version of emacs, in such a way it may lose attachment to originating tty, can abuse environment variables to communicate an originating shell's tty to elisp.
This works for me using Aquamacs in Mac OS X. Launching from a bash shell:
$ MY_TTY=$(tty) open /Applications/Aquamacs\ Emacs.app &
then in emacs:
(append-to-file "here I come to save the day\n" nil (getenv "MY_TTY"))

How do you run iex from Emacs?

I keep on getting this warning when I run iex using elixir-mode-iex from Emacs:
Warning: could not run smart terminal, falling back to dumb one
I think that this just means that I don't get tab completion, which I'm fine with. But I'd like a smart terminal if it's possible with elixir-mode in Emacs.
elixir-mode-iex uses the comint-mode major mode to interactive with iex. That also means that it's acting just like a dumb terminal (doesn't have the ability to process special escape sequences etc see here).
As a workaround you just could use term which sends any key press directly to the subprocess itself. You could write a function like the following:
(defun my-elixir-iex ()
(interactive)
(term "iex"))
I'm working on an iex Alchemist.el integration, which brings functionality like Inf-Ruby has. But until it's done try to just use iex via term
Cheers
Samuel
It looks like that warning occurs when IEX can't find tty support. You can enable tty mode in emacs by invoking it with -nw.

Why can't I set mark on emacs?

I am trying to run Emacs on a guest system (Ubuntu 12.04)
inside a Windows 8 command console.
This is done through vagrant + Virtual box.
It looks like this,
Guest system : Ubuntu 12.04
Host system : Windows 8
Environment : Virtual Box + Vagrant
Emacs works fine except I can not set mark on text (C-SPC).
I supposed there is some kind conflict with the key binding.
So I tried and added this to init.el,
(global-set-key (kbd "C-8") 'set-mark-command)
It doesn't work either.
Would someone please advice me?
If you're running Emacs in a terminal, then C-SPC and C-8 are unlikely to send what you want them to.
Use C-h c C-SPC to ask Emacs what it's seeing.
Try using C-# instead.
FYI, I found the Cygwin X server excellent for running GUI Emacs in a Windows-hosted VM. With GUI Emacs you can use many more key combinations than a terminal emulator will permit.
If you try that, start the server with startxwin rather than startx (the former enables the X server's windows to appear as regular Windows windows), and then connect from an xterm with ssh -X or ssh -Y (see the man page) to connect with X11 forwarding.
Thanks a lot for the hint.
I finally figured out what the problem really is.
From the answer #phils, this link clarifies the problem.
Why does Ctrl+. not work when I bind it to a command in Emacs?
In my case, all C-[any other key] bindings work, except for the set-mark-command.
I found a workaround.
By adding the following code to .emacs.d\init.el I can use F8 to set mark now.
(global-set-key [f8] (quote set-mark-command))
It is still confusing why in other command, Ctrl+key bindings works.