Building a Discourse Forum Client for Emacs

3 min read By Glenn Thompson
techemacselispdiscourseforums

Building a Discourse Forum Client for Emacs

As an Emacs enthusiast and active participant in various Discourse forums, I found myself wanting a more integrated way to interact with these communities directly from my favorite editor. This led me to create discourse-emacs, an Emacs package that enables users to browse, read, and interact with Discourse forums without leaving their editor.

Overview

The package provides a seamless interface to Discourse forums, currently being tested with the System Crafters community forum. It offers several key features:

  • Browsing latest topics
  • Viewing forum categories
  • Reading topic threads
  • Replying to discussions
  • Secure authentication using .authinfo

Technical Implementation

The package is built using Emacs Lisp and interacts with Discourse forums through HTTP requests, parsing the HTML responses directly rather than using a formal API. This approach, while more challenging, allows the package to work with any Discourse forum without requiring API keys or special configuration.

Key libraries used include:

(require 'request)  ; For HTTP requests
(require 'shr)     ; For HTML rendering
(require 'dom)     ; For HTML parsing
(require 'compat)  ; For compatibility across Emacs versions
(require 'auth-source) ; For secure credential management

Authentication

One of the key aspects of the package is secure authentication. Instead of storing credentials in plain text, it uses Emacs' built-in auth-source library. Users can store their credentials in .authinfo or .authinfo.gpg files:

(defcustom discourse-auth-sources '("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")
  "List of files to search for Discourse credentials."
  :type '(repeat string)
  :group 'discourse)

The package handles authentication by simulating a normal web browser login process, maintaining session cookies for subsequent requests.

User Interface

The package implements several specialized modes for different views:

  1. A topics list mode for browsing recent discussions
  2. A categories mode for navigating forum structure
  3. A topic mode for reading and responding to threads

Each mode comes with its own keymap, providing intuitive navigation:

(defvar discourse-base-map
  (let ((map (make-sparse-keymap)))
    (define-key map "g" 'discourse-get-latest-topics)
    (define-key map "q" 'quit-window)
    (define-key map "n" 'next-line)
    (define-key map "p" 'previous-line)
    (define-key map "b" 'discourse-show-categories)
    (define-key map (kbd "RET") 'push-button)
    map))

Session Management

The package maintains session state using cookies, storing them securely between sessions:

(defcustom discourse-cookies-directory "~/.discourse-cookies/"
  "Directory to store Discourse cookie files."
  :type 'directory
  :group 'discourse)

Current Status and Future Plans

The package is currently in active development, with version 0.2.0 focusing on core functionality with a single forum. Future enhancements might include:

  • Multi-forum support
  • Topic creation
  • Better HTML parsing and rendering
  • Notifications integration
  • Offline mode with local caching

Getting Started

To use the package, you'll need Emacs 27.2 or later and a few dependencies:

  • request 0.3.0
  • compat 29.1.4.2
  • markdown-mode 2.6

The source code is available on Codeberg.

Conclusion

Building discourse-emacs has been an interesting journey into both the non-existent Discourse API and Emacs package development. It showcases how Emacs can be extended to integrate with modern web platforms while maintaining the efficiency and keyboard-driven interface that Emacs users love.

The project is open source and welcomes contributions. Whether you're interested in adding features, improving documentation, or reporting bugs, feel free to get involved!