Repository navigation
Spot NFD File Names
The same name can be stored two ways. が can be one character (composed,
NFC) or か followed by a combining voiced mark (decomposed, NFD).
Names made on a Mac, or copied from one, are often NFD; most other software
writes NFC. The two look the same on screen, and that is the trouble: a
Windows or Linux tool, a web upload or a Git repository can see two
different names, and nothing in a file list tells you which one you have.
XeFM shows, sorts and searches every name composed, so the pane agrees with what you type at an IME. That is also why an NFD name looks like any other name in it. This recipe adds a mark after the names stored NFD, a way to select them, and a way to rename them to NFC.
It uses ATTRIBUTES and LAYOUT_EXTRAS, which are
preview
features (XeFM 1.11.0 or later). They may still change in a release.
Open Tools → Edit Configuration…. Put the functions above
class Config:, and merge the three tables into the ones your Config
already has:
import unicodedata
from xefm.user_api import Cell
def is_nfd(entry):
"""Whether the name on disk is not composed — `entry.name` is always
composed, `entry.path.name` is what the filesystem holds."""
return not unicodedata.is_normalized('NFC', entry.path.name)
def name_form(directory, entries):
return ['NFD' if is_nfd(e) else None for e in entries]
def nfd_mark(value, entry):
return Cell('NFD', color='orange') if value else ''
def select_nfd_names(ctx):
"""Select the items whose names are stored decomposed (NFD)."""
n = ctx.pane.select(is_nfd)
ctx.message(f"Selected {n} NFD name(s)")
def compose_names(ctx):
"""Rename the selected NFD names to their composed (NFC) spelling."""
targets = [e for e in ctx.pane.selected() if is_nfd(e)]
if not targets:
ctx.message("No NFD names selected")
return
def rename(ok):
if not ok:
return
# Where names are compared byte for byte (Linux), both spellings can be
# two files side by side; renaming onto the other would replace it.
on_disk = {e.path.name for e in ctx.pane.entries}
for e in targets:
composed = unicodedata.normalize('NFC', e.path.name)
if composed in on_disk:
ctx.message(f"{e.name}: an NFC file of that name already exists")
continue
try:
e.path.rename(e.path.with_name(composed))
except Exception as exc:
ctx.message(f"{e.name}: {exc}")
ctx.pane.refresh()
ctx.confirm(f"Rename {len(targets)} name(s) to NFC?", on_result=rename)
class Config:
ATTRIBUTES = {
'name_form': {
'label': 'Name form', 'type': 'enum', 'read': name_form,
'where': ['local', 'archive', 'ssh', 's3'],
'values': {'NFD': {'label': 'Name stored decomposed (NFD)',
'color': 'orange'}},
},
}
LAYOUT_EXTRAS = {
'nfd': {
'label': 'NFD names',
'markers': [{'attr': 'name_form', 'format': nfd_mark,
'on': 'name', 'side': 'after'}],
},
}
ACTIONS = {
'select_nfd_names': select_nfd_names,
'compose_names': compose_names,
}Run Tools → Reload Configuration (reload_config), or restart XeFM.
An NFD name gets an orange NFD after it:
がnfc .txt 0B 26-10-31 09:35
がnfd NFD .txt 0B 26-10-31 09:35
The other rows are not shifted: a mark takes no room on rows without one, and in a folder with no NFD names the list looks exactly as before. F1 (Help) lists the mark under Markers in this pane while one is showing.
View → Show ▸ NFD names turns it off for a tab and on again. It starts at
Auto, which here means everywhere: local folders, archives, SSH and S3.
Reading a name costs nothing anywhere, so the where list asks every kind of
storage.
Both actions are in the command palette (Tools → Command Palette…) under their docstrings:
- select_nfd_names adds every NFD name in the pane to the selection, so you can copy, move or check those files as a group.
- compose_names renames the selected NFD names to NFC, after a confirmation. Selected names that are already NFC are left alone. It skips a name whose NFC spelling is already a separate file in the folder, which can happen on Linux, where the two spellings are two different names.
Batch rename can't do this for you. It works on composed names, and only renames a file when its pattern changes the name, so a name that differs only in its form is left as it is.
To give either action a key, add it to your KEY_BINDINGS:
KEY_BINDINGS['select_nfd_names'] = ['Ctrl-Shift-N']entry.name, entry.stem and entry.suffix are always composed, so a filter
or sort key that compares them matches what you type. entry.path.name is the
name as the filesystem stores it. Use is_nfd(entry) above, or
entry.attr('name_form') once the pane has worked the value out, wherever you
need to tell the two apart.
Related: File List Layouts, Customization, issue #790.