Garjola Dindi
06 May 2023

AI assistants in Emacs. Don't use ChatGPT. Help Open Science.

Everybody seems to be very excited about generative AI models these days. The availability of the Large Language Models (LLMs) through conversational interfaces like ChatGPT and of image generation tools like Dall-E or Stable Diffusion have brought generative AI to the masses. Although Github Copilot, also based on the GPT family of models, has been available for a while now, this was a niche tool, for programmers only.

Of course, Emacs being the best text-based interface to a computer, it is also the best interface to the generative AI models which are driven through textual prompts. Everything being a text buffer in Emacs, sending and receiving text via an API is straightforward (if you know Emacs Lisp).

It is therefore not a surprise that there are many Emacs packages allowing to use ChatGPT and Copilot. Just to list a few1:

Recently, David Wilson at System Crafters did a live stream showing some of the capabilities of these packages. To be honest, I was not impressed by the results of the code generated by the LLMs, but I can understand that many people may find them useful.

At the end of the stream, David wanted to address the question of the problems and issues with using these models. Of course, being a programmer, David likes recursion, and he asked ChatGPT about that. The LLM answered, as usual, with a balanced view with pros and cons. It is also usual for these models, in my opinion, to give awfully banal answers. There can be legal issues, copyright ones (in both senses, that is, that the models are trained with copyrighted material, and that the copyright of their outputs is not well defined), ethical problems, etc.

As always, the Silicon Valley tech firms have not waited for these issues to be settled before deploying the technology to the public. They impose their vision regardless of the consequences. Unfortunately, most programmers like the tech so much that they don't stop thinking before adopting and participating in spreading it.

Emacs being one of the most important contributions of the Free Software community, it may be surprising that some of its users are so keen to ride the latest Trojan horse of technofeudalism. Fermin pointed out a similar kind of issue with the Language Server Protocol, but that was not a real problem, since LSP servers run locally and we have free software implementations of them. This is not the case for ChatGPT or Copilot. We only have an API that can be taken down at any moment. But worse than that, as pointed out, using these LLMs means that we are working in training them. Every time that David, in his demo, wrote "… the code is wrong because of …, can you fix it?", he was giving feedback for the reinforcement learning of ChatGPT.

So what can we do? Stop using these tools and loose our programming or writing jobs because we will be less productive than those that use them?

Maybe we can do what GNU hackers and other Free Software activists have always done: implement libre tools that are digital common goods that free and empower users. Building and training big AI models is very costly and may be much more difficult than building an OS kernel like Linux, GCC or Emacs, but fortunately, there are already alternatives to ChatGPT.

The best starting point could be helping the BigScience Project by using their Bloom model from Emacs to train it and improve it. Hints on how to install Bloom locally and use it with Python can be found in this blog post. There are also initiatives to use federated learning2 to improve Bloom. The BigCode project targets code generation, and is to BigScience what Copilot is to ChatGPT. You can play with it here. There is a VSCode plugin for their StarCoder model, but no Emacs package yet. Isn't that a shame?

BigScience is Open Science, while OpenAI's is not open, but proprietary technology built from common digital goods harvested on the internet. It shouldn’t be difficult for us, Emacs users, to choose who we want to help.

Footnotes:

1

In alphabetical order using M-x sort-lines 😉

2

Yes, federated like in Fediverse!

Tags: emacs ai chatgpt open-science
02 Oct 2022

A workaround for an annoying EXWM behavior

I have been using the Emacs X Window Manager, EXWM, for a couple of years now and I have come to depend on it for an efficient and friendly workflow for most of what I do at the computer. Before that, I had been using StumpWM for 4 or 5 years and I was already sold on tiling window managers. For someone like me who does everything in Emacs except for browsing sites that need Javascript or watching videos, EXWM seems to be the perfect setup.

The pain point with EXWM is that its development and maintenance suddenly stopped after the creator and maintainer went AWOL. There have been discussions here and there to continue the development, but, to the best of my knowledge, no initiative has really taken off.

Some well known EXWM users in the community started migrating to other WMs, like StumpWM, Herbstluftwm or or QTile. I also tried to get back to StumpWM, since I had used it for a long time, but I found the experience inferior to EXWM: having M-x everywhere and not having to use different key bindings when talking to the WM or to Emacs is a real comfort.

However, some time ago, floating windows started behaving annoyingly. For instance, when saving a file in Firefox, the file chooser application would be too high and the Save button would be below the bottom of the screen. I then would have to move the window up so that I could click. Not knowing anything about X, I wasn't able to diagnose the issue.

I ended writing a little function to resize a floating window:

(defun my/resize-floating-frame (width height)
  "Resize a floating exwm frame to WIDTHxHEIGHT"
  (interactive "nWidth: \nnHeight: ")
    (let ((floating-container (frame-parameter exwm--floating-frame
                                               'exwm-container)))
      (exwm--set-geometry floating-container nil nil width height)
      (exwm--set-geometry exwm--id nil nil width height)
      (xcb:flush exwm--connection)))

and bound it to a key to call it with a size that would fit:

(exwm-input-set-key (kbd "s-x r") (lambda () (interactive) (my/resize-floating-frame 600 800)))

So now, instead of grabbing the mouse and manually moving the window, I could just use s-x r and hit enter, since the Save button is selected by default. This is a big win, but after a couple of days, I became tired of this and thought that this could be automated. Also, the arbitrary fixed size may not be appropriate depending on the monitor I am using.

So with a little more elisp, I wrote a function that computes the size of the window and works with a hook which is called by EXWM and the end of the setup of the floating window:

(defun my/adjust-floating-frame-size ()
  "Ensure that the current floating exwm frame does not exceed the size of the screen"
  (let* ((frame (selected-frame))
         (width (frame-pixel-width frame))
         (height (frame-pixel-height frame))
         (w1 (elt (elt exwm-workspace--workareas 0) 2))
         (h1 (elt (elt exwm-workspace--workareas 0) 3))
         (w2 (elt (elt exwm-workspace--workareas 1) 2))
         (h2 (elt (elt exwm-workspace--workareas 1) 3))
         (max-width (round (* 0.75 (min w1 w2))))
         (max-height (round (* 0.75 (min h1 h2))))
         (final-height (min height max-height))
         (final-width (min width max-width)))
    (set-frame-size frame final-width final-height t)))
(add-hook 'exwm-floating-setup-hook #'my/adjust-floating-frame-size 100)

All this may seem complex and one could think that too much elisp-foo is needed to do all this. This is absolutely not the case. With a little effort and time, it is not difficult to navigate the source code and test things interactively to see how they behave. In this particular case, searching for hooks in the EXWM package (M-x describe-variable) and trying little code snippets on the *scratch* buffer got me there rather quickly.

This is yet another demonstration of the power of Emacs: you can modify its behavior interactively and all source code and documentation is immediately accessible. You don't need to be a skilled programmer to do that.

That's user freedom, the real meaning of software freedom.

Tags: emacs exwm bugs
08 Apr 2022

Read e-books from your Calibre library inside Emacs

I read many books in electronic format. I manage my e-book library with the great Calibre software. For non fiction books (science, philosophy, productivity, computer science, etc.) I like to read on my laptop, so I can easily take notes, or actually generate Zettels. Since I create new zettels with org-capture, reading inside Emacs is very handy. For EPUB format, I use the excellent nov.el and for PDF files I use pdf-tools. Both of these packages provide org links, so I don't have to do anything special to capture notes and have a back-link to the point in the e-book where the note was taken from. This is standard org-capture.

I also use org links as bookmarks between reading sessions. Yes, that is bookmarks as marks in the book, like real, paper books.

The part in my reading workflow where Emacs wasn't used was managing the e-book library. Usually, when I got a new book, I would open Calibre, add the book, fetch missing meta-data and cover if needed and then close Calibre. After that, I would use calibre-mode.el to call calibre-find to search for the book and insert an org link into my reading list.

I have been aware of calibredb.el for a couple of years (or maybe a little less), but I had not used it very much. After a recent update of my packages, I had a look at it again and I discovered that it provides all the features I use in the Calibre GUI, so I have started to use it to add books and curate meta-data and covers.

calibredb has a function to open a book from the list: calibredb-open-file-with-default-tool which in my case runs ebook-viewer for EPUB and evince for PDF. I did not find an option or a variable to change that, so I looked at the code of the function:

(defun calibredb-open-file-with-default-tool (arg &optional candidate)
  "Open file with the system default tool.
If the universal prefix ARG is used, ignore `calibredb-preferred-format'.
Optional argument CANDIDATE is the selected item."
  (interactive "P")
  (unless candidate
    (setq candidate (car (calibredb-find-candidate-at-point))))
  (if arg
      (let ((calibredb-preferred-format nil))
        (calibredb-open-with-default-tool (calibredb-get-file-path candidate t)))
    (calibredb-open-with-default-tool (calibredb-get-file-path candidate t))))

This function calls calibredb-open-with-default-tool, which looks like this:

(defun calibredb-open-with-default-tool (filepath)
  "TODO: consolidate default-opener with dispatcher.
Argument FILEPATH is the file path."
  (cond ((eq system-type 'gnu/linux)
         (call-process "xdg-open" nil 0 nil (expand-file-name filepath)))
        ((eq system-type 'windows-nt)
         (start-process "shell-process" "*Messages*"
                        "cmd.exe" "/c" (expand-file-name filepath)))
        ((eq system-type 'darwin)
         (start-process "shell-process" "*Messages*"
                        "open" (expand-file-name filepath)))
        (t (message "unknown system!?"))))

So in GNU/Linux, it calls xdg-open, which delegates to the appropriate applications. But of course, I want to use Emacs.

I could use xdg-settings to change the behavior, but that would mean using emacsclient which seems weird given that I am already inside Emacs (actually, I use EXWM, so it is still weirder). Furthermore, since my Emacs is configured to open EPUB files with nov.el and PDF with pdf-view, I only need to call find-file on the selected file.

So I just adapted calibredb-open-file-with-default-tool and bound it to the same key as the original function:

  (defun my/calibredb-open-file-with-emacs (&optional candidate)
    "Open file with Emacs.
Optional argument CANDIDATE is the selected item."
    (interactive "P")
    (unless candidate
      (setq candidate (car (calibredb-find-candidate-at-point))))
    (find-file (calibredb-get-file-path candidate t)))
  (define-key calibredb-search-mode-map "V" #'my/calibredb-open-file-with-emacs)

This is yet another demonstration of the power of Emacs: packages that were not designed to work together can be combined exactly as you want. You don't need to be a skilled programmer to do that.

That's user freedom, the real meaning of software freedom.

Tags: emacs calibre epub nov books
02 Apr 2022

Install Emacs with Conda

At work, I frequently use a high performance computing cluster (300 nodes, 1000 CPU, 44 TB of RAM). It is an amazing environment. Many users access the system via Jupyter Hub and Virtual Research Environments where many tools for development are available. However the recommended editor is VSCode and I need Emacs.

I access the system via ssh on a terminal and run Emacs without the GUI. This is enough for me, since I have all I need:

  1. Window multiplexing using Emacs windows
  2. Virtual desktops thanks to tab-bar-mode
  3. Persistence across sessions using desktop-save-mode
  4. IDE features with eglot
  5. A data science environment with org-mode's babel

This is great, since I can use the same tools I use on my laptop, with the same configuration and be really efficient.

Unfortunately, a couple of months ago, the web proxy that we have to go through to access the internet from the cluster was upgraded and the old Emacs version available on the system was not able to fetch packages.

Doing some research, I found that Emacs 27 could be configured to work. Since I did not want to bother the sysadmins without being sure that it would work, I decided to compile Emacs from source as I do on my laptop.

Unfortunately, I couldn't manage to get all dependencies working. So I needed to find another way to have a recent Emacs on the system.

On the cluster, we use Conda to build virtual environments for scientific computing. With Conda, we can install whatever package we want at the user level without any particular privilege.

So I just did:

conda install emacs

and I got the last stable Emacs release, which is 27.2 at the time of this writing.

So if you need to use Emacs in a system where you can't install system wide packages and you don't want or can't build it yourself, Conda can be a good solution. Installing Conda does not need particular privileges.

Tags: emacs conda
12 Mar 2022

Some Org Mode agenda views for my GTD weekly review

GTD Weekly review

What's GTD

I am an extremely lazy and messy person. As such, in order to survive, I need a system to find my way out of bed in the morning, have something to eat in the fridge, don't forget to pick my children at school, feed the cat and things like that.

More than 15 years ago, when I already was an Emacs user, I heard about Getting Things Done (GTD for short), a book and a personal productivity system that promised to save my life. I bought the book and started implementing the system right away. If you don't know anything about GTD, reading the Wikipedia entry will give you a good overview. In short, the action management part of the system relies on the following steps:

  1. Capture everything you have to remember or solve.
  2. Periodically empty your inboxes (what you have captured, and any other input you get as e-mail, physical stuff, etc.).
  3. Transform every input into either actionable things, reference material or trash. Actionable things can be projects (need more than one action), next actions (everything is ready to do what you need to do) or things to delegate.
  4. Maintain lists for actions and projects.
  5. Periodically review your lists.
  6. Don't forget to do some of your actions from time to time!

This is a crude description of the system, but it will be enough for the rest of this article.

The funny thing is that one could imagine that, as an avid Emacs user, I looked for ways to implement GTD with it. But the real story is that I discovered GTD because, when I was looking for ways to manage actions with Emacs I found what I think is the absolute reference on the use of Org Mode to get organized, and GTD is cited there.

Implementing GTD with org-mode

My implementation is much inspired by Bernt Hansen's suggestions in the above-mentioned site. For actions, I use the following states:

(setq org-todo-keywords 
      (quote ((sequence "TODO(t!/!)" "NEXT(n!/!)" 
                        "STARTED(s!)" "|" "DONE(d!/!)")
              (type "PROJECT(p!/!)" "|" "DONE_PROJECT(D!/!)")
              (sequence "WAITING(w@/!)" "SOMEDAY(S!)"  "|"
                        "CANCELLED(c@/!)"))))

The meaning of these states is:

NEXT
an action that I need to do, for which I have estimated the effort, and for which I have everything to start acting.
TODO
an action that I need to do but that I don't want to see in my next actions list.
STARTED
a next action for which some progress has been made.
DONE
a finished action.
WAITING
an action to be done, but for which I am waiting for some input or some action from somebody else.
SOMEDAY
an action that could be interesting to do, but on which I don't want to commit yet.
CANCELLED
an action that I decided that I finally don't need to do.

The projects are just containers for related actions.

Next actions are very important. They are what allows to achieve progress even in periods of crisis or low energy. All my actions have tags for contexts. These can be general (work, admin, errands) or specific (the name of a particular project or person, a type of activity (reading, programming, writing). This allows to easily generate lists of actions which match a set of tags. The final thing that makes a next action special is the effort estimation. Using Org Mode properties, I assign an effort estimation to each next action, so that I can also filter by estimated duration when I have limited time available.

Since I spend most of my time in Emacs, using the Org Agenda makes being organised an easy and pleasant experience. The Org Agenda is my main dashboard that I trigger with C-c a. I think this is the default key binding, but I have this in my Emacs config:

(define-key global-map "\C-ca" 'org-agenda)

My Org Agenda is built from more than 60 Org Mode files.

The GTD weekly review

One key part of maintaining a clean GTD system is the weekly review. Once a week, I sit down for 90 minutes to 2 hours and I go through this checklist:

  • [ ] Re-schedule / re-deadline today's tasks
  • [ ] Collect loose papers and materials
  • [ ] Mental collection
  • [ ] Get ins to zero
    • Questions :
      • What is it?
        • Actionable?
          • Yes
            • Next action?
              • 2minutes? (do it)
              • delegate? (waiting for)
              • defer it to a specific date?
              • or next action to do it ASAP (tag next)
            • Is it multiple actions?
              • Project
          • No
            • Reference material (tag reference)
            • Incubate (remind me later) (tag incubate)
            • Trash it
          • No
  • [ ] Get current
    • [ ] Review next actions list
      • [ ] what might have been important may not be it anymore
      • [ ] check that we have effort for all
    • [ ] Go back to previous calendar data
      • [ ] things which got rescheduled or should
      • [ ] actions which were not done
    • [ ] Review upcoming calendar
    • [ ] Review waiting for list
    • [ ] Stuck projects
    • [ ] Look at project list
    • [ ] Review relevant checklists
  • [ ] Get creative
    • [ ] review someday/maybe
    • [ ] generate new ideas

For some of these steps, I have Org Agenda custom commands or specific agenda views which are very useful. I am sharing them below.

Reschedule / redeadline today's tasks

After a week without reviewing my system, undone actions begin to pile-up and my dashboard is very crowded. The first step is therefore reschedule or cancel some actions. For that, I generate a Daily Action List with the following Org Agenda custom command:

(setq org-agenda-custom-commands
      (quote
       (("D" "Daily Action List"
         ((agenda ""
                  ((org-agenda-span 1)
                   (org-agenda-sorting-strategy
                    (quote
                     ((agenda time-up category-up tag-up))))
                   (org-deadline-warning-days 7))))
         nil)
        ;; [....] 
        )))

With C-c a D, I get all the actions for the current day and all undone things which were scheduled earlier or for which the deadline has passed, will also appear. The actions with a deadline in the coming 7 days will also show up.

Get current

The first step in getting current, is looking at the list of all next actions. For this, I have a group of custom commands. There is one for all actions, and 2 others which select only work or only home actions respectively:

Review next actions list

(setq org-agenda-custom-commands
      (quote
       (
        ;; [....]
        ("n" . "Next Actions List")
        ("nn" tags "+TODO=\"NEXT\"|+TODO=\"STARTED\"")
        ("na" tags "+home+admin+TODO=\"NEXT\"|+home+admin+TODO=\"STARTED\"")
        ("nh" tags "+home+TODO=\"NEXT\"|+home+TODO=\"STARTED\"")
        ;; [....]
        )))

I can see all my next actions (with TODO state being NEXT or STARTED) by typing C-c a n n.

Check that we have effort for all

As I said above, I want to have an effort estimate for all my next actions. I use org-ql to generate an agenda view with all next actions that don't have Effort property. I display that with the following interactive command:

(defun my/show-next-without-effort ()
  (interactive)
  (org-ql-search (org-agenda-files)
    '(and (todo "NEXT")
          (not (property "Effort")))))

Go back to previous calendar data and Review upcoming calendar

One crucial exercise in the weekly review is to have a look at the past week and the coming ones so that we see what we have done, what we should have done but didn't, and what's coming ahead.

For this, I also generate an agenda view with 24 days, starting 10 days ago. This way I see the last week and a half and the next 2 weeks. I have built the following interactive command to do that:

(defun my/generate-agenda-weekly-review ()
  "Generate the agenda for the weekly review"
  (interactive)
  (let ((span-days 24)
        (offset-past-days 10))
    (message "Generating agenda for %s days starting %s days ago"
             span-days offset-past-days)
    (org-agenda-list nil (- (time-to-days (date-to-time
                                           (current-time-string)))
                            offset-past-days) 
                     span-days)
    (org-agenda-log-mode)
    (goto-char (point-min))))

Putting the agenda in log mode, allows to see the tasks marked as DONE at the corresponding time of closing. If, like me, you clock all your working time, the task will appear also every time it was worked on. This is great to get a sens of what was accomplished.

Review waiting for list

Reviewing all the actions that are blocked because they are delegated or need an input from someone else is important. Org Mode allows to generate an agenda view for that with the standard agenda dispatcher. Typing C-c a T generates an agenda view with all tasks matching a particular TODO keyword. I enter WAITING at the prompt and that's all!

Stuck projects

Sometimes, a project (that is, something that needs several actions to be done) gets stuck. Org Mode has a concept of stuck project (see the description of the variable org-stuck-projects), but I found it difficult to configure to my liking. I use org-ql again to generate a custom command like this:

(setq org-agenda-custom-commands
      (quote
       (
        ;; [....]
        ("P" "Stuck Projects"
         ((org-ql-block '(and (todo "PROJECT")
                              (or (not (descendants))
                                  (and (not (descendants 
                                             (or (todo "STARTED") (todo "NEXT"))))
                                       (descendants 
                                        (and (or (todo "TODO") (todo "WAITING")) 
                                                         (not (deadline)) 
                                                         (not (scheduled)))))
                                  (not (descendants (or (todo "NEXT") (todo "TODO") 
                                                        (todo "WAITING") 
                                                        (todo "STARTED")))))))))
        ;; [...]
        )))

For me, a stuck project is a headline with a PROJECT keyword that does not have a next action, or for which the non next actions don't have a deadline or a scheduled time slot.

Look at project list

To generate the list of projects, I use the same approach as for the list of next actions.

(setq org-agenda-custom-commands
      (quote
       (
        ;; [....]
        ("p" . "Projects List")
        ("pp" tags "+TODO=\"PROJECT\"")
        ("pw" tags "+TODO=\"PROJECT\"+work")
        ("ph" tags "+TODO=\"PROJECT\"+home")
        )))

Typing C-c a p p gives me the list of all my projects.

Review someday/maybe

Having a list of things you may want to do someday is exciting. You can add whatever you want knowing that you don't have to commit to it unless or until you feel like it. However, sometimes you forget for too long about all those things and reviewing them can give new ideas or put you in the mood to commit.

The main problem with a someday/maybe list is that it keeps growing, so you can't review it weekly. Mine contains more than 2650 items! What I do is that I randomly jump to one point in the list and review items for 5 minutes. I use the following interactive command:

(defun my/search-random-someday ()
  "Search all agenda files for SOMEDAY and jump to a random item"
  (interactive)
  (let* ((todos '("SOMEDAY"))
         (searches (mapcar (lambda (x) (format "+TODO=\"%s\"" x)) todos))
         (joint-search (string-join searches "|") )
         (org-agenda-custom-commands '(("g" tags joint-search))))
      (org-agenda nil "g")
      (let ((nlines (count-lines (point-min) (point-max))))
        (goto-line (random nlines)))))

It seems a little convoluted, since I could have done the same thing as for the waiting for list, but with this command I can use other TODO states by adding them to the list in the first line of the let. Also, the jump to a random point is cool.

Conclusion

As you can see, I have a custom set of commands and functions to make my weekly review easy. This is important. Until I automated this things, I would tend do skip the review too often. Now that I have a streamlined workflow I am much more disciplined with my weekly reviews and this results in an increased peace of mind.

My elisp code may need cleaning and improvements. Do no hesitate to send me some feedback by e-mail if you have any suggestion or tricks to share.

Tags: emacs gtd org-mode agenda
Other posts
Creative Commons License
dindi.garjola.net by Garjola Dindi is licensed under a Creative Commons Attribution-ShareAlike 4.0 Unported License. RSS. Feedback: garjola /at/ garjola.net Mastodon