Einar's blog

Python Mode in Emacs on Windows

Python mode in Emacs on GNU/Linux works out of the box with no setup if you have Python installed on your system. However, on Windows, Python mode's functionality to open the Python shell and import the code in the active buffer into the shell does not work out of the box even if Python is installed. It does not help to add the directory Python is in to the exec-path or add it to the $PATH environment variable either. After trying those two things, I looked for a useful variable to set the path to the python executable and tried a couple of promising ones before finding the correct solution. It is very simple, but it is still extra setup that you don't need on any other OS than Windows.

This is the Elisp you need in your init.el (or whatever you call the file that ultimately loads your config):

(setq python-shell-interpreter "c:/Python312/python.exe")

Another thing I discovered today is that Windows 11 has a Beta feature to switch to UTF-8 text encoding. I hope Microsoft prioritises this feature and switch to proper line endings (LF) at the same time to make Windows slightly less incompatible with the rest of the world. Most other OSes set UTF-8 and POSIX line endings as their defaults 20 years ago. It's a feature that would make sense, unlike removing functionality and reimplementing it again or adding enshittification of the kind where the user's default browser is not respected when you search the internet through the Start menu (to add eyballs to Bing advertising through Edge), which seems to be Microsoft's development strategy for Windows 11 thus far. UTF-8 and LF line endings would make Windows less painful to use for developers and sysadmins.

PowerShell in Emacs Shell Mode on Windows

I recently decided to give native Emacs on Windows another go (instead of using it with WSLg with its dysfunctional Wayland stack on WSL2 with Ubuntu 24.04). Since I have to use Windows for some of the classes I teach, I have tried to like WSL2. Microsoft implements Wayland in a very non-standard way that is totally broken, and even after applying a fix (not from Microsoft even if they have known for months that their default distro doesn't work with WSLg, but from users in the GitHub bug report), I had trouble with the Emacs window freezing after having locked my screen and unlocked it. I do this at every break during the day, and there are 7 of those every day, so this has to work. So I thought I would try to go native again with the Windows version of Emacs and just use what I need right now and gradually fix or work around the issues that needs fixing since Windows does things in incompatible and weird ways. For now, I need Shell Mode to work, so I have looked at that first.

By default, you get cmd.exe in Shell mode on Windows. I had some trouble finding good documentation of how to set it up with PowerShell. Some suggest just setting PowerShell to explicit-shell-file-name, but that only partially worked since it gave me an error that PowerShell was invoked with a wrong argument. A lot of people pointed to someone on the Microsoft blogs that set up PowerShell in Shell mode, but in a way that removed the prompt. He had made an Elisp workaround to get the prompt back, but it seemed like there should be an easier way. The trouble is that comint mode, the mode shell mode is built on, sends -i by default to the shells it interacts with as an argument. He had found a variable to send exactly the parameters he wanted to PowerShell, and sent something strange to make PowerShell remove the prompts. But that is the opposite of what I want. So I tried a couple of things and it turns out the solution is really simple.

(use-package shell
  :config
  (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
  (add-to-list 'comint-output-filter-functions 'ansi-color-process-output)
  (add-hook 'comint-output-filter-functions 'comint-osc-process-output)
  (setq explicit-shell-file-name "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe")
  (setq explicit-powershell.exe-args '(""))
  (add-hook 'shell-mode-hook (lambda() (company-mode 0))))

In the code block above, I turn on ansi-color since PowerShell can work with those. I tell Emacs to run PowerShell and give it the arguments "" (ie, nothing) which remove the defautl argument -i, but adds nothing else. And now I have PowerShell in Shell mode. The last line is useful for me because I have turned on company globally, but I don't want it in shell mode.

I also discovered that if I use the command wsl inside PowerShell inside Shell Mode inside native Windows Emacs, I can use Bash on my WSL2 Ubuntu 24.04 from inside Powershell in Shell mode, although I don't get a prompt from Bash. This is actually useful for me since I want to keep the number of windows as small as possible when using Windows since its window handling is so poor (compared to my usual Sway tiling WM setup on GNU/Linux) and I need one program from WSL2 to copy some information that I then need to use inside PowerShell to do some of my daily tasks at work. With this hack, I don't need to launch a window with Ubuntu (or WSLg Emacs with Shell mode) to interact with that CLI program that runs in Bash on WSL2 (and not directly on Windows in PowerShell).

Insecure sudo settings in RaspberryPi OS

I have a RaspberryPi 5. Since Debian does not supply an image for use with the RPi5 and Ubuntu Server comes with a lot of software I do not want preinstalled, I use RaspberryPi OS Lite and run my own script to set it up with Sway and the other packages I like. It is very similar to Debian even if it adds an extra repo, some settings that are different and a few programs for changing settings, both directly related to the hardware and some that replicate standard tools like localectl, passwd, systemctl enable etc in a more convenient TUI.

For a while, I have wondered why I only needed to write my password once when using sudo and then never again. It means that if you are logged in as your user, you have all the privileges of root if you just once wrote your password. It is convenient, but it is like logging in with the root user which is not a very good idea.

I had a look at visudo, the command to change the settings for sudo and everything looked quite normal. Except that in the end, the file sources any file in the directory /etc/sudoers.d/. So naturally, I had a look in that directory. There are some files there that cause problems. The first one is 010_pi-nopasswd which sets up sudo to never time out the sudo password typed by the first user created on the system.

Another is that sudo isn't per shell, terminal or tty, but global. This is in the file /etc/sudoers.d/010_global_tty. Usually, you would have to type your password to get sudo privileges in each terminal, shell or tty, but on RaspberryPi OS, sudo accepts the password I typed once in tty1 in every shell, terminal or tty.

Finally, there is a file called 010_proxy which accepts sudo from any proxy logged in as the first user, ie since I typed a password once in tty1, any proxy logged in as me doesn't have to type a password ever again to escalate privileges with sudo. Not the best setting either.

To mitigate the problem, you should make sure you are added to the sudo group by typing sudo usermod -aG sudo username in a terminal and press return, but replace username with your actual username. This is because we are going to delete the file that lets your users type your sudo password only once, but still want your user to be able to use sudo. The sudoers file on RaspberryPi OS is set up so users in the sudo group can use sudo.

Then you should write sudo rm /etc/sudoers.d/010_pi-nopasswd and return to delete the file that gives your user the ability to only type your password once. Continue with sudo rm /etc/sudoers.d/010_global-tty and return to remove the settings that doesn't ask for a new password in every tty, terminal or shell. Go on with sudo rm /etc/sudoers.d/010_proxy and return to avoid proxies not having to write a password. Finally, write sudo visudo and look for a line that says Defaults env_reset. Move your cursor to the end of that line with the cursor keys and hit return to get a new line. Write Defaults timestamp_timeout=5 to get a five minute timeout for sudo. Then press ctrl-o, return when it asks for a filename to save to and then ctrl-x to quit Nano. (RaspberryPi OS does not respect your settings for $EDITOR and will use Nano.)

My installscript for Debian checks if I am on RaspberryPi OS and does the things I have listed above, except add the timeout in the sudoers file. It is supposed to be edited only with visudo, so I tell the user to do this manually in the end of my script. (It also checks if the user is on Debian or RaspberryPi OS and installs Firefox on RaspberryPi OS and Firefox-ESR (Extended Support Releas) on Debian. (I tried installing Firefox from Debian Unstable first, but there were dpendency problems with that approach.) Emacs is installed from Backports to get then newest one backported to work with Debian Stable, whether you are on RaspberryPi OS or Debian. In the past, I used the Emacs snap, but Emacs from backports is faster and I trust the Debian maintainers more than the proprietary Snap Store. Since Debian has a stricter interpretation of what Free Software is than the FSF, I also have to install the non-free repos to install Emacs documentation.)

DOSbox and SDL error on Wayland

I tried installing DOSbox tonight to run some old games from the 90s. On first run, I got Exit to error: Can't init SDL No available video device as output in my shell (Shell mode in Emacs). I use a Raspberry Pi 5, so my first thought was to look for video drivers, but Mesa is already installed. I looked up the SDL packages I had installed and seemed to have everything needed. After an extensive search that did not really give me any solution, I tried something someone in a forum had tried, but in a slightly different way than they did and it actually worked.

I echoed out $SDL_VIDEODRIVER and got wayland as my answer since I run the tiling window manager Sway. (This is also the normal answer if you run Gnome (Ubuntu, OpenSUSE, Fedora…), KDE Plasma (Kubuntu, KDE Neon…), Wayfire (Raspberry Pi OS), Qtile on Wayland or Hyperland.) I checked to make certain I had xwayland installed and then tried launching DOSbox again with SDL_VIDEODRIVER=X11 dosbox in a terminal and it worked. It will probably work for other programs that use SDL that won't launch on Wayland as well.

My search for solutions found a lot of users of different programs that used SDL that had trouble with getting their programs to run and with no credible solution given in any of the forums I visited. I hope this blog post will help people in that situation.

I also hope programs made to run on X11 start to wake up to the reality that most GNU/Linux users use Wayland now and adjust their code. Most GUI FOSS use Qt or GTK which has worked on Wayland since Qt5 and GTK3 without any new code. The programs that need updating are programs not using those toolkits. For those still not convinced of the need to adapt to a new reality: All the major distros ship Gnome which is Wayland by default. Most GNU/Linux users are on those distros. KDE Plasma used by many of the people not using Gnome is also Wayland by default. Raspberry Pi OS uses Wayfire as their desktop now (they used LXDE before even if they called it something else) and it uses Wayland. And there are many of us tiling WM users on Wayland as well (Wayfire, Hyperland, Qtile and Sway).

X11 is very old, bloated and has not been in active development since June 6, 2012 except for security fixes. Gnome and Plasma spent a long time getting their compositors good enough for prime time and that delayed the transition for a decade and left a lot of users thinking Wayland wasn't ready while many other users, me included, were using the Wayland protocol with the wlroots compositor on Sway with few issues every day. MATE, XFCE and LXQt were cooperating on a Wayland compositor based on Lumiri's (Unity8's) Mir last time I checked, so soon, everyone except a few tiling WM users will be on Wayland. I am looking forward to not having to install Xwayland when all the program developers and package maintainers catch up to today's reality in a decade's time…

Interactive presentations

I often present new material for my students in class. I teach programming and information technology at a vocational school, so I often need to run code as examples of what I am presenting about. Sometimes, I also want to tweak the code slightly and rerun it within the flow of my presentation. Another thing I like to do is to make my presentations interactive. I might ask my students something and write down their answers in my presentation or I might hand out individual tasks, collect the answers and use the presentation to share the results with everyone on the screen. (We have large 4k touch screens that also have whiteboard functionality in every classroom.)

We have MS365 (formerly known as MS Office) at the school and most of my colleagues use PowerPoint for their presentations. But PowerPoint cannot be used for interactive presentations. Either you create the presentation or you present it. There is no mode for presenting it while adding text or editing code blocks. It is also unable to run code when I am presenting. The workaround would be to run the code in some other tool like the python shell, node.js, the developer tools in a web browser or whatever REPL or compiler is available for the language you present about and take a screenshot and put that into your presentation. But I want to run the code after I have explained the concepts I am trying to get through so my students can see that it actually works the way I told them. I also like to tweak the code if they need an additional explanation or to try out different approaches (for instance in JavaScript: myObject["key"] or myObject.key) PowerPoint has the same problems with interactivity as LibreOffice Present, Google Docs whatever it is called this week, Apple Keynote and all the other standard presentation software. They are all unsuitable for presentations if there is any kind of interactivity. And when you are in a classroom, you want interactivity to keep your audience engaged.

Emacs Org mode is a tool for outlining ideas (and a lot of other stuff like calendaring, todos and project planning) and there is support for inline images and code blocks in almost any programming language that you can run when you want (or tangle out to a separate code file a la literate programming which is how I make my literate Emacs config, but that is another blog post for another day). It is not presentation software, but Emacs is extendable and hackable enough that it can be made to work as presentation software. Ric Lister has made an Emacs package called org-present that does exactly what I need. I make my presentation in org mode as an org file (org markup is similar to markdown), add the images and code blocks directly within the text with the markup. When I am in class, I just run org-present with M-x. (Alt-x or Meta-x as it is called in Emacs for historical reasons lets you run any interactive function). Each top level heading becomes a slide. I can write directly into the slides while I present them since they are just text files in org markup displayed with larger font sizes and nicer headlines etc, which makes it possible to do things like ask my students something and write down their answers. I can also run code blocks by hitting Ctrl-C Ctrl-C with the pointer above the code block and the results pop up underneath in a separate Results block within the text of my presentation. Another advantage is that if I alt-tab into another program like Firefox, my presentation within Emacs does not switch out of presentation mode like most presentation software like PowerPoint tends to do. I can come back to my presentation after showing a Youtube video with Firefox and continue where I left off.

Since org-present allows interactivity, running code blocks and switching back and forth from other software, I tend to use presentations as outlines for whole classes now, not just for the parts that includes a traditional presentation where I present some new information, but I also put in headings for tasks that I put on our learning platform (it's learning). When I get to the tasks, I can alt-tab to Firefox and show the students where they are and tell them how to work on them, and then after we have spent whatever time I set aside for the tasks, I can continue with my presentation and maybe give them some new information from the bullet points, pictures or code blocks in the presentation or I can get their feedback on what they learned from the tasks and write that into the presentation.

After class, I export the org file of the presentation to a PDF and put it on our learning platform to make it easy for students not present to see what was happening or for later review before a test or an exam. I have set up org-present to work the way I like with a bit of code in my Emacs config. I turn off the menu bar, turn on display of inline images and set a standard width that usually works (that I can easily change with some metadata above an image) and use org-present-big to make everything large. I then hook my configuration into the start of org-present and also have a function that resets my configuration that I hook in when org-present is quit. I highly recommend org-present if you want interactivity within your presentations, especially if you already use Emacs and Org mode.



All content is shared under the terms of the Creative Commons Attribution-ShareAlike license.