Skip to content

Commit 74bbe58

Browse files
chdocpre-commit-ci[bot]nriley
authored
make Emacs filename detection work with the default window title (#2058)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Nicholas Riley <[email protected]>
1 parent aff07a1 commit 74bbe58

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

apps/emacs/emacs.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2+
import re
23
from typing import Optional
34

4-
from talon import Context, Module, actions, settings
5+
from talon import Context, Module, actions, settings, ui
56

67
mod = Module()
78
mod.setting(
@@ -362,8 +363,19 @@ def language():
362363

363364
@ctx.action_class("win")
364365
class WinActions:
365-
# This assumes the title is/contains the filename.
366-
# To do this, put this in init.el:
367-
# (setq-default frame-title-format '((:eval (buffer-name (window-buffer (minibuffer-selected-window))))))
366+
# This assumes the title either is the buffer name or contains the buffer
367+
# name before a space-surrounded hyphen. This is the default for the latest
368+
# GNU Emacs. If you are not on macOS and your flavor of Emacs defaults to
369+
# something incompatible, you may need to put one of the following two
370+
# declarations into your init.el ("%b" being replaced by the buffer name):
371+
# (setq frame-title-format "%b")
372+
# (setq frame-title-format '(multiple-frames "%b" ("" "%b - Emacs at " system-name)))
368373
def filename():
369-
return actions.win.title()
374+
# On macOS, get the filename directly
375+
if doc := getattr(ui.active_window(), "doc", None):
376+
return doc
377+
378+
# Otherwise, get it from the window title
379+
title = actions.win.title()
380+
buffer_name = title.split(" - ")[0]
381+
return re.sub(r"<[^>]+>$", "", buffer_name)

0 commit comments

Comments
 (0)