Apr 26,
2019

VirtualBox, Bluetooth and Suspend: Pick Two

Howdy Interweb neighbor.

I think I've witnessed a bizarre interaction between VirtualBox, Bluetooth and suspend mode on my desktop PC.

Sometimes, very randomly and with intervals between manifestations of the bug so long that I couldn't isolate the cause, the desktop is resumed from suspend without a working Bluetooth adapter and with a repeating error about Bluetooth in the kernel logs, but without useful panic or call trace messages.

I concede the above description has nothing bizarre about it, unless I should mention that the error persisted between reboots and also after shutdowns. The only way to restore normal functionality was pulling the power cord from the wall main socket after power-off.

Now, this is a thing one doesn't see everyday!

Other than installing the most recent BIOS, I begun testing every new kernel that mentioned changes in the Bluetooth stack and every time I installed an update it seemed to solve the issue, until after some time the error resurfaced. Before a few weeks ago tho, I never correlated the fact that the bug manifested itself only after I compiled the VirtualBox's I/O kernel module1

A couple of months have passed without VirtualBox installed and the system has resumed flawlessly dozens of times. I'm not beyond any doubt I've isolated the culprit — I'd need a reproducible way to trigger the bug to be unequivocally sure — but for the moment I'm pretty confident I've removed this issue.

Now I need to explore some other virtualization solution because I don't think this one would be an easy bug to trace and properly fix it as I would guess, from lack of similar reported cases online, an hardware quirk. Also if memory serves me well, it doesn't help the fact that VirtualBox is in a somewhat low "maintenance mode".


  1. whomever has used VirtualBox at least once knows what I'm talking about ↩︎

 
 

Apr 18,
2019

Why I use Zsh

Because "glob qualifiers" expansion with numeric sort order it's boss, that's why!

Greetings Netizens.

Today I was confronted with a silly problem, i.e. how to apply a series of patches in order where their name was ending with a numeric index.

For about ten files it's not a task that should deserve a blog post, the default glob expansion is sorted as anyone would expect:

$ for i in {0..9}; do touch foo-${i}; done
$ echo foo-*
foo-0 foo-1 foo-2 foo-3 foo-4 foo-5 foo-6 foo-7 foo-8 foo-9
$

But what would happens when dealing with a dozen or more files?

Well, for the benefit of the unaware reader, the default expansion uses the alphabetical sorting, or better the sorting is based on the lexicographical order of the ASCII/UTF1 characters encoding, as showed on the example below2:

$ for i in {0..15}; do touch foo-${i}; done
$ echo foo-*
foo-0 foo-1 foo-10 foo-11 foo-12 foo-13 foo-14 foo-15 foo-2 foo-3 foo-4 foo-5 foo-6 foo-7 foo-8 foo-9
$

Not the order that I would like the patches applied.

Bummer, right?

ʕ ಡ ﹏ ಡ ʔ

Well, one possible solution it's suggested in my own example, using a loop with a numeric index, also I'm adding a file existence check before the action:

$ for i in {0..15}; do [[ -f foo-${i} ]] && echo foo-${i}; done
foo-0
foo-1

[...]

foo-9
foo-10
foo-11
foo-12
foo-13
foo-14
foo-15
$

Feasible, even if a little verbose.

I'm sure someone of the Zsh team was bummed too because there's an option called NUMERIC_GLOB_SORT, not set by default tho, that literally states if numeric filenames are matched by a filename generation pattern, the filenames are sorted numerically rather than lexicographically. This means that one has to remember to check if the option is enabled or to apply it with setopt if not.

Can we do better? Sure, enter the glob qualifiers.

٩( ′ㅂ`)و

Briefly, with its qualifiers, Zsh can basically filter the result of a glob expansion, for example *bar*(/) returns a list of all the directories that contains the "bar" string in the current directory3. There is a huge list of qualifiers described on the official documentation and nearly at the end of the page there's the n qualifier which description states it sets the NUMERIC_GLOB_SORT option for the current pattern:

$ for i in {0..15}; do touch foo-${i}; done
$ echo foo-*
foo-0 foo-1 foo-10 foo-11 foo-12 foo-13 foo-14 foo-15 foo-2 foo-3 foo-4 foo-5 foo-6 foo-7 foo-8 foo-9
$ echo foo-*(n)
foo-0 foo-1 foo-2 foo-3 foo-4 foo-5 foo-6 foo-7 foo-8 foo-9 foo-10 foo-11 foo-12 foo-13 foo-14 foo-15
$

Neat, isn't?

Keybind of the Day

Not strictly related to the above, but I've recently found out that on Zsh there's a function called _most_recent_file bound as default to "Ctrl-x m". I think I've always had a couple of aliases on my .zshrc to list the last modified file or directory using — guess what — glob qualifiers4, but this is way better as it can select the most recent file and also expand a glob. For example cat *.py followed by "Ctrl-x m" would insert in the edit buffer the name of the last Python file modified in the current directory, even if more recent files are present.


  1. sorting UTF multibyte encoded characters has it's own can of worms ↩︎

  2. instead of Bash, Zsh can also expands in-line pressing two times the Tab key or Ctrl-i ↩︎

  3. also Zsh will recursively expand/search files from the current directory when using two glob symbols, like **bar*(/) ↩︎

  4. for the curious reader *(.om[1]) and *(/om[1]) respectively for files and directories, so the first element of the list sorted by modification date ↩︎

zsh
 
 

Mar 05,
2019

Emacs Context Menu

Howdy folks!

Today I want to talk about the Emacs context menu, or the lack of it on a default install.

Out of the box, when clicking the right mouse button on an Emacs' window in graphical mode nothing happens, well the mouse-save-then-kill function is invoked1 but it's not what one usually expect. I must admit that sometime I also fumbled with the mouse before reaching for the keyboard, usually when interacting with other applications like web browsers or file managers.

As a side note, speaking of interaction between Emacs and file managers, it never occurred to me that the Dired mode could react to drag&drop actions until I "randomly" copied a file to a currently opened Emacs' Dired window. I was quite surprised to see the file copied to the relative directory.

Try to replicate that vi.

Anywho, this is my take on the context menu in graphical mode:

My take on Emacs context menu

Well, I didn't want anything fancy, I like to keep things simple; if I'm using the mouse it's probably because I've copied a string from another application and I want to yank/paste2 it or the other way around, so that's what I was striving for.

For the interested reader, I'll put all the code below as it's quite minimal:

(global-set-key [mouse-3] 'my-context-menu)

(with-eval-after-load "menu-bar"
  (require 'url-util)

  (defvar edit-popup-menu
    '(keymap
      (undo menu-item "Undo" undo
            :enable (and
                     (not buffer-read-only)
                     (not
                      (eq t buffer-undo-list))
                     (if
                         (eq last-command 'undo)
                         (listp pending-undo-list)
                       (consp buffer-undo-list)))
            :keys "")
      (separator-undo menu-item "--")
      (cut menu-item "Cut" clipboard-kill-region
           :enable (use-region-p)
           :keys "")
      (copy-link menu-item "Copy Link" 
                 (lambda () (interactive) (kill-new (url-get-url-at-point)))
            :enable (and (url-get-url-at-point))
            :keys "")
      (copy menu-item "Copy" clipboard-kill-ring-save
            :enable (use-region-p)
            :keys "")
      (paste menu-item "Paste" clipboard-yank
             :keys "")
      (paste-from-menu menu-item "Paste from Kill Menu" yank-menu
                       :enable (and
                                (cdr yank-menu)
                                (not buffer-read-only))
                       :help "Select a string from the kill ring and paste it")
      (clear menu-item "Delete" delete-region
             :enable (and mark-active (not buffer-read-only))
             :help "Clear region"
             :keys "Del")
      (separator-select-all menu-item "--")
      (mark-whole-buffer menu-item "Select All" mark-whole-buffer
                         :enable (not (= (buffer-size) 0)))))

  (defun my-context-menu (event)
    "Pop up a context menu."
    (interactive "e")
    (popup-menu edit-popup-menu)))

(provide 'context-menu)

That's all.


  1. on a terminal it should pop up the terminal emulator context menu but that depends on the terminal emulator implementation ↩︎

  2. my defaults are the traditional Alt-w/Ctrl-w for copy/kill and Alt-y for yank ↩︎