Autocompletion for C and C++ with Emacs 24

I have already done a blog post on auto completion using Emacs. But that was back in Emacs 23 days. Long ago…

Since then a lot has happened. Emacs 24 has been released, package managers like MELPA or ELPA have become standard, and company-mode seems to be winning against auto-complete. Also, clang has made huge strides forward.

So it is time to revisit the task of developing C or C++ using Emacs. I have put online an easy-to-install Emacs init.el that you can use as a start for your own development environment. I am using the OS X version of Emacs, but this should also work on Linux, given that you have clang and git installed.

You can install this init.el by issuing the following commands:

cd
git clone https://github.com/root42/yet-another-emacs-init-el
mkdir ~/.emacs.d
cd ~/.emacs.d
ln -snf ../yet-another-emacs-init-el/init.el .

The file will make sure that upon startup of Emacs all necessary packages are installed, like company, magit but also LaTeX tools like aucTeX or refTeX. You can disable this check (or individual packages) in the init.el.

The code is up on github, so feel free to fork and/or contribute.

When doing simple C++ programming using only standard libraries, you should be ready to go. For more complex projects, you have to tweak the clang parameters, so that the compiler will find the header files. Completion happens automatically after “.“, “->” and “::” but also when pressing M-/. You can rebind this key in the init.el, of course. This is what the completion looks like:

My most commonly used keys that I have re-bound are as follows:
  • f3 – Runs ff-find-other-file, trying to switch between header and implementation for C/C++ programs.
  • f4 – Toggles the last two used buffers.
  • f5, f6 – If tabbar is enabled (tabbar-mode), navigates back/forward through tabs.
  • f7 – Toggle ispell dictionaries (german/english).
  • f8 – Kill current buffer.
  • f9 – Run compile.
  • M-? – Run grep.
  • M-n – Go to next error in compilation buffer.
  • M-S-n – Go to previous error in compilation buffer.
  • M->, M-< – Go to next/previous Emacs frame.
  • M-/ – Run autocompletion using company mode.
  • C-x o, C-x C-o – Go to next/previous Emacs window

Very nice is also the magit-mode, which is a very sane interface to git for Emacs. It looks like this:

You can run it by executing M-x magit-status. Just type ? to get online help. Magit uses simple one-character-commands, like s for stage, c for commit, p for push and so on.

Currently I am evaluating the integration of lldb into Emacs, but haven’t come far enough to say that I have found a powerful and flexible interface, apart from the standard command line. So there’s more to come, hopefully!

How to backup your Raspberry Pi SD-Card

In the following I will explain on how you can backup your Pi’s SD-card, so that whenever it breaks down, you can simply restore the backup image to a fresh SD-Card. This may also help with tinkering, when you totally screwed up your Pi’s Linux-installation. I will explain the first step specifically for Mac OS X, but you can do this similarly on other UNIXes and Linux, by using the mount and umount commands.

So, shut down your Pi using the command sudo shutdown -h now and remove the SD-card after the Pi has done so. Take the card and insert it into your Mac’s SD-card slot.

First of all, we need to find out which disk device has been assigned to the card. We can do this with diskutil list:

user@mymacintosh:~ $ diskutil list                                                                
/dev/disk0                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:      GUID_partition_scheme                        *512.1 GB   disk0                             
   1:                        EFI EFI                     209.7 MB   disk0s1                           
   2:                  Apple_HFS M4                      511.1 GB   disk0s2                           
   3:                 Apple_Boot Recovery HD             784.2 MB   disk0s3                           
/dev/disk1                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:     FDisk_partition_scheme                        *15.9 GB    disk1                             
   1:             Windows_FAT_32                         58.7 MB    disk1s1                         
   2:                      Linux                         15.9 GB    disk1s2                           

Then unmount the card using diskutil unmountDisk:

user@mymacintosh:~ $ diskutil umountDisk /dev/disk1                                              
Unmount of all volumes on disk1 was successful                                                     

Next we will use dd to produce an image, which we can compress using bzip2, or pbzip2 for more performance. You can use MacPorts to install pbzip2.

user@mymacintosh:~ $ sudo dd if=/dev/disk1 | pbzip2 > raspberry_pi_$(date “+%Y-%m-%d”).img.bz2 
31116288+0 records in                                                                                                           
31116288+0 records out                                                                                                        
15931539456 bytes transferred in 2347.692010 secs (6786043 bytes/sec)                                                           

Restoring the image is also easy:

user@mymacintosh:~ $ bunzip2 raspberry_pi_2014-03-22.img.bz2 | sudo dd of=/dev/disk1      

Getting Emacs for Mac OS X to honour the shell environment

If your are using the excellent Emacs for Mac OS X distribution, you may have noticed that it does not use the environment you may have defined in your ~/.bashrc. This can be a problem if you are using MacPorts, for example to install clojure and Leiningen. You will get an error message like this, when trying to run inferior-lisp in clojure-mode:

apply: Searching for program: No such file or directory, lein

This can be avoided by using the excellent exec-path-from-shell package for Emacs. It is available via (M)ELPA, just install it using list-packages, or download it from github. You can enable it, especially for OS X, by putting this in your init.el:
(when (memq window-system '(mac ns))
(exec-path-from-shell-initialize))

How to use find and rsync to copy a bunch of files to a local or remote destination

I use rsync a lot. And I use find a lot. You can use them both together, by using the magnificent xargs command. Two problems are there to solve: xargs usually just pastes the arguments from find at the end of the specified command. No way to specify the destination directory. Second, file names with spaces are a problem.

The solutions are:

  • The -print0 option of find uses 0-bytes to separate results, instead of spaces and newlines
  • The -0 option of xargs tells xargs to look for said 0-bytes
  • The -J option of xargs allows us to tell xargs where to put the file names that we feed to it.

So let’s put it together:

pi@raspberrypi ~ $ find . -name ‘your*pattern.jpg’ -print0 | xargs -J % -0 rsync -aP % user@host:some/dir/

Thanks to redeyeblind for the insightful blog post.

How to resize tmux windows using OS X terminal

A while back I pointed out a blog post by someone else, which described how to get C-left and C-right working in tmux and OS X. Now I noticed that I cannot resize windows in tmux. This is because tmux seems to expect xterm keys for C-arrow. The xterm keycodes for the arrow keys are:

  • left key: ^[[D
  • right key: ^[[C
  • up key: ^[[A
  • down key: ^[[B
  • C-left: ^[[1;5D
  • C-right: ^[[1;5C
  • C-up: ^[[1;5A
  • C-down: ^[[1;5B

I can now configure OS X terminal to send those key codes, and tmux works fine. However, other apps running in tmux will break, because they don’t expect to get xterm key codes. I found a workaround in the ArchWiki, which suggests to create your own terminfo entry. I will try that and report back here.

Update: The solution is adding two lines to the ~/.tmux.conf file:

set -g default-terminal “xterm-256color”
setw -g xterm-keys on                   
Update 2: And here is my OS X Terminal.app configuration:

Update 3: And a very last update… It seems that tmux does not support bce (background color erase), which xterm does. This is a problem for progams like htop, vim, or mc. You will see rendering errors, if you do not fix this.

So you need to make your own terminfo file and own terminal type, called xterm-256color-nobce. You can do this on the command line:

pi@raspberrypi ~ $ infocmp xterm-256color | sed ‘s/bce, //’ | sed ‘s/xterm-256color/xterm-256color-bce/g’ > xterm-256color-nobce

This will create a new terminfo source file, which does not advertise the bce feature. You can install this with the following command:

pi@raspberrypi ~ $ sudo tic ./xterm-256color-nobce

This makes the terminal type xterm-256color-nobce available. After this, change your ~/.tmux.conf once again, to use the new terminal type per default:

set -g default-terminal “xterm-256color-nobce”

Update 4: To make the Ctrl+Arrow keys also work in regular OS X Terminal, you need to edit or create ~/.inputrc to contain this:

# xterm keys for skipping a word
“e[1;5C”: forward-word
“e[1;5D”: backward-word
“e[5C”: forward-word
“e[5D”: backward-word

The “1;” variant is otherwise not recognized by GNU readline on OS X.

How to copy Mails from iCloud to GMail

You can use the fabulous imapsync tool to copy mails between IMAP servers. For example you can copy a certain folder from Apple’s iCloud to Google’s gmail:

imapsync 
--noauthmd5 --ssl1 --ssl2
--host1 mail.me.com --user1 'your.icloud.name'
--host2 imap.gmail.com --user2 'your.gmail.name@googlemail.com'
--folder 'your/folder/to/be/copied' --sep1 '/'
--prefix1 '' --prefix2 '[Google Mail]' --sep2 '/'

The important parts here are the user names for the IMAP servers. Note that you need to generate an application specific password, if you are using Google two factor authentication! Also important is the “[Google Mail]” IMAP prefix.

Edit: It seems gmail has a weird interpretation of all the IMAP folders and stuff. Since they are using labels, the above script might create a weird label for the copied emails, but they will be there nevertheless!

OS X 10.6.5: GLSL problems are solved

Thank you Apple, thank you. The latest OS X update solved my crashes and broken renderings when using GLSL. Apple does not state what has changed in the knowledge base, but my last assumption was that at least the GLSL compiler produced broken code. Apple uses the wonderful LLVM compiler infrastructure for GLSL, which is great, but also known to produce problems sometimes. But LLVM+GLSL has made great advances, and this update makes it even better. Here’s the correct rendering which I now can enjoy:

Update: I was too quick with my joy. The machine crashed again. But differently. It took much longer, and there was a hint screen this time, plus the machine sent a crash report after the reboot. Hopefully we are slowly getting to a solution… Ok, let’s reopen the bugreport at Apple…
Update 2: I investigated a little bit more. It seems that when I switch the MBP back to using the GeForce 9400M instead of the 9600M, the machine does not crash. I played around for a couple of minutes, and it seems to work fine. Of course it is horribly slow, because I am pushing it with a massive volume dataset and a marching raycaster, but it at least works. The 21″ iMac with the ATI card still fails miserably. Rendering is totally borked, but at least it does not crash.
Update 3: After two weeks of letting this rest, I tried today again, and I cannot manage to crash the whole machine anymore. I don’t know what Updates changed this behaviour, but I am glad anyway. The ATI card still produces garbage, but neither the 9400M, nor the 9600M GT nor the GT120 in the iMac crash at the moment. If the machine locks up again, I’ll report back with the panic log.