HomeAboutPostsTagsProjectsRSS

Updated
Words655
TagsRead2 minutes

Using lsp-bridge for Language Server Protocol in Emacs

Emacs 29 introduced built-in TRAMP Docker support for editing files within containers. However, enabling auto-completion inside a container remains challenging.

After discovering lsp-bridge , I realized the benefit of a Python process interacting with the language server, rather than relying solely on Emacs Lisp.

I added a feature for automatic reconnection to remote SSH servers and started supporting devcontainers. Using nix-darwin to configure my MacBook, I implemented the devcontainer feature to install lsp-bridge and the language server within the devcontainer. This was facilitated by the Nix ecosystem . To make it work, I needed to start with patching the official Nix devcontainer feature and create separate projects — a long journey, but worthwhile.

Container File Buffer Handling

When opening files in a container, lsp-bridge creates an lsp-bridge buffer, inserts the file content, and uses it for editing and rendering diagnostic information. The remote file content on the container is maintained by the lsp-bridge server running inside the container.

I completed the handling of the visited file modification time for the buffer and enabled auto-revert mode to reload file content automatically when the formatter updates the file.

Using the created lsp-bridge buffer, I used set-visited-file-name to associate a buffer with a file path. However, saving the buffer after the first save prompts a warning: “File has been changed since visited.” This occurs due to discrepancies in how file timestamps are handled by Emacs through TRAMP. To resolve this, Emacs’ record of the file’s modification time must be manually updated with something like this:

(defun my-update-file-mod-time ()
  (when buffer-file-name
    (let ((mod-time (file-attribute-modification-time (file-attributes buffer-file-name))))
      (set-visited-file-modtime mod-time))))

(add-hook 'after-save-hook 'my-update-file-mod-time)

Emacs’ “auto-revert” mode, which automatically reverts buffers when the displayed files change externally, is also useful when files are updated by the formatter. Therefore, the file buffer must enable auto-revert-mode.

With these pieces in place, I now have a smooth editing experience with auto-completion inside the devcontainer.

Emacs rocks!

Updated
Words95
TagsRead1 minute

When taking notes, friction is often a good thing. If it is too easy to create a new note, you don’t write something on your own, but rather just collect information. Consequently, your brain do not process the information, so you learn less from just collecting and reading it. Physical notes have a physical constraint and a limitation on how much we can write on them, but digital notes have no such limitation and rely on self-discipline. So, when taking notes, it is too easy to write too many words. Keep it short and simple.

Updated
Words288
TagsRead1 minute

I use vertico-postframe to place my completion window at the center of screen, however when doing incremental search like doom-emacs SPC s s, it will block the man window.

Here is how to temporarily disable vertico-posframe-mode in Emacs before executing a function, like an incremental search, and then re-enable it afterward.

(defun my-search-without-posframe ()
  "Perform a search without `vertico-posframe-mode' temporarily."
  (interactive)
  ;; Disable vertico-posframe-mode if it's enabled
  (when (bound-and-true-p vertico-posframe-mode)
    (vertico-posframe-mode -1)
    (unwind-protect
        ;; Perform the search
        (call-interactively '+default/search-buffer)
      ;; Re-enable vertico-posframe-mode
      (vertico-posframe-mode 1))))

in config.el

(map! :leader
      :desc "Search without posframe"
      "s s" #'my-search-without-posframe)

Updated
Words329
TagsRead1 minute

different height for Org mode headings

modify only the height of headings in Org mode without affecting other attributes like color

Instead of using custom-theme-set-faces which replaces the whole face definition (and could unintentionally override theme-specific settings like color), use set-face-attribute

doom-emacs config example

(after! org
  ;; Adjust the height of org headings upon org-mode startup
  (add-hook 'org-mode-hook (lambda ()
    (dolist (face '((org-level-1 . 1.75)
                    (org-level-2 . 1.5)
                    (org-level-3 . 1.25)
                    (org-level-4 . 1.1)
                    (org-level-5 . 1.05)
                    (org-level-6 . 1.0)
                    (org-level-7 . 0.95)
                    (org-level-8 . 0.9)))
      (set-face-attribute (car face) nil :height (cdr face)))))

Updated
Words485
TagsRead1 minute

Emacs-obsidian

might be too overkill for me , just opening note in Emacs for intensive editing is enough.

from [[Emacs]]: quickly open Obsidian note

use Deft Mode

from [[Obsidian]]: open current file in Emacs

open file and go to line command for emacsclient to open a.txt at line 4 column 3

emacsclient +4:3 a.txt

use obsidian open current file in emacs

emacsclient -c -s work +{{caret_position}} {{file_path:absolute}}

Use shell-command plugin to create a obsidian command to run the shell command

Updated
Words65
TagsRead1 minute

Posting a markdown document directly to Telegram will display formatting like code blocks and bold text in the message. However, the content received by the Telegram bot differs from what is initially sent, particularly with the removal of backticks.

To send the message content as plain text without formatting, I first paste it into Apple Note on iOS and then share it with the Telegram bot.

Updated
Words482
TagsRead1 minute

Modern Emacs: all those new tools that make Emacs better and faster - YouTube

Optimization

  • Lexical Binding: Before Emacs 24 emacs-lisp is dynamic binding. lexical binding is introduced in Emacs 24, enabling compile-time optimizations.
  • Native Compilation: Introduced in Emacs 28, native compilation used the GCC JIT compile to convert Emacs Lisp code to native machine code, dramatically improving execution speed.

Package Management

  • Lazy loading with use-package: use-package is now build into Emacs 29
  • Straight Package Manager : works with use-package to handle package installations and version control.

Syntax Highlighting and Parsing

  • Tree-sitter Integration: Emacs 29 includes support for Tree-sitter, a parser generator that improves syntax highlighting and code navigation by using precise parse trees instead of regular expressions. This results in more accurate and faster syntax highlighting.
  • tree-sitter mode: new major modes that utilize tree-sitter for enhanced code parsing and highlighting, for example, use python-ts-mode to replace traditional python-mode.

Completion Frameworks

  • Historical Context: Initially, Emacs used ido for interactive file and buffer navigation, followed by heavier frameworks like helm and ivy for more advanced completions.
  • Modern Solutions: vertico consult orderless marginalia embark

Language Server Protocol (LSP)

  • eglot: Emacs’ build-in LSP client

I personally use lsp-bridge for completion and LSP

Updated
Words409
TagsRead1 minute

Found a hacked way to use local submodule directory as nix package source, so that I might separate different configuration using different git repos.

in flake.nix add local submodule as input

# flake.nix
inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    darwin.url = "github:lnl7/nix-darwin";
    darwin.inputs.nixpkgs.follows = "nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    # submodule directory
    config-sketchybar-src = {
      url = "git+file:./config-sketchybar";
      flake = false;
    };
};

config-sketchybar is a local git submodule, we can define a package using it as source.

pkg-sketchybar-config.nix

{ inputs, pkgs, ... }:

pkgs.stdenv.mkDerivation {
  name = "sketchybar-config";

  dontConfigure = true;
  dontUnpack = true;
  src = inputs.config-sketchybar-src;

  installPhase = ''
    mkdir -p $out
    cp -r $src/config/sketchybar/* $out

    # Find all .sh files and substitute 'jq' with the full path to the jq binary
    find $out -type f -name "*.sh" | while read script; do
      substituteInPlace "$script" \
        --replace 'jq' '${pkgs.jq}/bin/jq'
    done

    chmod -R +x $out
  '';
}

and pkg-sketchybar-config can be included in home.nix as regular package.

#nix-darwin

Updated
Words295
TagsRead1 minute
# Function to download and process subtitles
download_and_process_subtitles() {
    local video_url="$1"
    local base_filename="$2"

    # Download subtitles using yt-dlp
    yt-dlp --write-auto-subs --sub-format "vtt" --skip-download -o "${base_filename}.%(lang)s.%(ext)s" "$video_url"

    # Find the downloaded subtitle file
    local subtitle_file=$(ls ${base_filename}.*.vtt | head -1)

    # Check if the subtitle file exists
    if [ -f "$subtitle_file" ]; then
        # Process the subtitle file to remove tags, timestamps and duplicate lines
        perl -ne 'print unless /<.*>/ or /^\s*$/ or /-->/' "$subtitle_file" | awk '!seen[$0]++' > "${base_filename}.txt"
        echo "Processed subtitle saved as ${base_filename}.txt"
    else
        echo "No subtitle file found."
    fi
}

# Variables
VIDEO_URL="video url"
BASE_FILENAME="basename"

# Call the function
download_and_process_subtitles "$VIDEO_URL" "$BASE_FILENAME"

Updated
Words1132
TagsRead2 minutes

[[Obsidian]] works amazingly well on iOS, it is really satisfying to write notes on mobile with it. Along with iOS shortcuts and automation, using Obsidian streamlines your mobile note-taking experience like never before.

Use [[iOS shortcut]] to automate tasks

Obsidian Advanced URI | Obsidian Advanced URI

This plugin exposes api to Obsidian via schema URL, combing it with iOS shortcuts make it possible to perform complex automation, like creating note, appending note and reordering list (via Sort & Permutation plugin).

There is a Call Command parameter for calling command from other plugins, which is the key component to automate with iOS shortcuts.

example: add to list and reorder list alphabetically

select some text and use share menu, select the shortcut.

add to the heading TIL on daily note

use commands from various plugin to do following steps:

obtain the command id

command id can be obtain by using Advanced URI: copy URI for command on the Command Palette

Actions URL

Also look at the Actions URL I find it works more reliably and has the ability to run dataview query

same workflow on macOS

The same shortcut workflow used on iOS can be reused on macOS via services, amazing cutting the boilerplate of manually copy & pasting note taking process.

CSS for multiple fonts

Font Atkinson Hyperlegible

Download the Atkinson Hyperlegible Font | Braille Institute

Use this excellent font for low vision reading , perfect for using with Obsidian on iPhone!

Custom Font Loader

Enable Custom Font Loader plugin, enable multiple fonts.

Custom CSS of using different fonts for default and code block.

:root * {
  --font-default: Atkinson-Hyperlegible-Regular-102a;
  --default-font: Atkinson-Hyperlegible-Regular-102a;
  --font-family-editor: Atkinson-Hyperlegible-Regular-102a;
  --font-monospace-default: Atkinson-Hyperlegible-Regular-102a,
  --font-interface-override: Atkinson-Hyperlegible-Regular-102a,
  --font-text-override: Atkinson-Hyperlegible-Regular-102a,
  --font-monospace-override: Atkinson-Hyperlegible-Regular-102a, 
 }

.reading * {
font-size: 20pt !important;
}
/* reading view code */
.markdown-preview-view code {
font-family: 'Iosevka';
}
/* live preview code */
.cm-s-obsidian .HyperMD-codeblock {
font-family: 'Iosevka';
}
/* inline code */
.cm-s-obsidian span.cm-inline-code {
font-family: 'Iosevka';
}

references

How to increase Code block font?! - #2 by ariehen - Custom CSS & Theme Design - Obsidian Forum

How to update your plugins and CSS for live preview - Obsidian Hub - Obsidian Publish