# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "User Environment"
        USER[User]
        SHELL[Shell/Terminal]
        DOOMDIR["$DOOMDIR<br/>(User Config)"]
    end

    subgraph "GNU Emacs Runtime"
        EMACS[GNU Emacs 27.1+]

        subgraph "Doom Emacs Core"
            EARLY[early-init.el]
            DOOM[doom.el]
            DOOM_START[doom-start.el]
            DOOM_LIB[doom-lib.el]
            DOOM_CLI[doom-cli.el]
        end

        subgraph "Feature Layers"
            DOOM_UI[doom-ui.el]
            DOOM_EDITOR[doom-editor.el]
            DOOM_KEYBINDS[doom-keybinds.el]
            DOOM_PROJECTS[doom-projects.el]
        end

        subgraph "Package Management"
            DOOM_STRAIGHT[doom-straight.el]
            STRAIGHT[straight.el]
        end

        subgraph "Module System"
            MODULES[modules/]
            MOD_LANG[lang/*]
            MOD_TOOLS[tools/*]
            MOD_UI[ui/*]
            MOD_EDITOR[editor/*]
            MOD_OTHER[...]
        end
    end

    subgraph "External Dependencies"
        GIT[Git]
        RIPGREP[ripgrep]
        FD[fd]
        LSP[LSP Servers]
        REPOS[Package Repos<br/>GitHub/MELPA/ELPA]
    end

    USER --> SHELL
    SHELL --> EMACS
    USER --> DOOMDIR

    EMACS --> EARLY
    EARLY --> DOOM
    DOOM --> DOOM_LIB
    DOOM --> DOOM_START
    DOOM_START --> DOOM_UI
    DOOM_START --> DOOM_EDITOR
    DOOM --> DOOM_CLI

    DOOM_LIB --> DOOM_STRAIGHT
    DOOM_STRAIGHT --> STRAIGHT
    STRAIGHT --> GIT
    GIT --> REPOS

    DOOM_START --> MODULES
    MODULES --> MOD_LANG
    MODULES --> MOD_TOOLS
    MODULES --> MOD_UI
    MODULES --> MOD_EDITOR
    MODULES --> MOD_OTHER

    MOD_TOOLS --> RIPGREP
    MOD_TOOLS --> FD
    MOD_LANG --> LSP

    DOOMDIR --> DOOM_START
```

## 起動シーケンス図

```mermaid
sequenceDiagram
    participant Emacs as GNU Emacs
    participant Early as early-init.el
    participant Doom as doom.el
    participant Start as doom-start.el
    participant Profile as Profile Init
    participant Modules as Modules
    participant User as User Config

    Emacs->>Early: Load early-init.el
    Note over Early: GC optimization<br/>file-handler optimization

    Early->>Doom: Load doom.el
    Note over Doom: Define constants<br/>Load doom-lib.el

    Doom->>Doom: doom-initialize()
    Note over Doom: Set doom-context<br/>Run doom-before-init-hook

    Doom->>Start: Load doom-start.el
    Note over Start: Runtime optimizations<br/>UI element setup

    Start->>Profile: Load profile init file
    Note over Profile: Load doom-ui.el<br/>Load doom-editor.el

    Profile->>Modules: doom-before-modules-init-hook
    loop Each Module
        Modules->>Modules: Load init.el
    end
    Modules->>Modules: doom-after-modules-init-hook

    Modules->>Modules: doom-before-modules-config-hook
    loop Each Module
        Modules->>Modules: Load config.el
    end
    Modules->>Modules: doom-after-modules-config-hook

    Modules->>User: Load $DOOMDIR/config.el
    User->>User: Load custom.el

    User->>Emacs: doom-after-init-hook
    Note over Emacs: Start gcmh-mode<br/>Display benchmark
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "User Layer"
        CONFIG["$DOOMDIR/config.el"]
        PACKAGES_USER["$DOOMDIR/packages.el"]
        INIT_USER["$DOOMDIR/init.el"]
    end

    subgraph "Module Layer"
        subgraph "Application Modules"
            APP[app/*]
            EMAIL[email/*]
        end
        subgraph "Language Modules"
            LANG[lang/*<br/>67+ languages]
        end
        subgraph "Tool Modules"
            TOOLS[tools/*]
            CHECKERS[checkers/*]
        end
        subgraph "UI Modules"
            UI_MOD[ui/*]
            THEMES[themes/*]
        end
        subgraph "Editor Modules"
            EDITOR_MOD[editor/*]
            COMPLETION[completion/*]
        end
    end

    subgraph "Feature Layer"
        UI[doom-ui.el]
        EDITOR[doom-editor.el]
        KEYBINDS[doom-keybinds.el]
        PROJECTS[doom-projects.el]
    end

    subgraph "Package Layer"
        STRAIGHT_INT[doom-straight.el]
        PACKAGE_MACRO["package! macro"]
    end

    subgraph "Core Layer"
        LIB[doom-lib.el]
        COMPAT[doom-compat.el]
        CLI[doom-cli.el]
    end

    subgraph "Bootstrap Layer"
        EARLY_INIT[early-init.el]
        DOOM_CORE[doom.el]
        START[doom-start.el]
    end

    subgraph "Platform Layer"
        EMACS_RT[GNU Emacs Runtime]
    end

    CONFIG --> UI_MOD
    PACKAGES_USER --> PACKAGE_MACRO
    INIT_USER --> LANG

    APP --> UI
    LANG --> EDITOR
    TOOLS --> LIB
    UI_MOD --> UI
    EDITOR_MOD --> EDITOR

    UI --> STRAIGHT_INT
    EDITOR --> STRAIGHT_INT
    KEYBINDS --> LIB
    PROJECTS --> LIB

    STRAIGHT_INT --> LIB
    PACKAGE_MACRO --> STRAIGHT_INT

    LIB --> DOOM_CORE
    COMPAT --> DOOM_CORE
    CLI --> DOOM_CORE

    EARLY_INIT --> EMACS_RT
    DOOM_CORE --> EARLY_INIT
    START --> DOOM_CORE
```

## モジュールシステム構成図

```mermaid
graph LR
    subgraph "Module Definition"
        DOOM_MACRO["doom! macro"]
        MODULE_KEY["(:group module)"]
        FLAGS["+flag1 +flag2"]
    end

    subgraph "Module Structure"
        subgraph "Module Files"
            INIT[init.el]
            CONFIG_EL[config.el]
            PACKAGES_EL[packages.el]
            AUTOLOAD[autoload.el]
            DOCTOR[doctor.el]
        end
        DOOMMODULE[.doommodule]
    end

    subgraph "Module Registry"
        DOOM_MODULES[doom-modules<br/>hash-table]
        MODULE_CONTEXT[doom-module-context]
    end

    subgraph "Load Order"
        INIT_PHASE[1. Init Phase]
        CONFIG_PHASE[2. Config Phase]
        AUTOLOAD_PHASE[3. Autoload Phase]
    end

    DOOM_MACRO --> MODULE_KEY
    MODULE_KEY --> FLAGS
    FLAGS --> DOOM_MODULES

    DOOM_MODULES --> MODULE_CONTEXT
    MODULE_CONTEXT --> INIT
    MODULE_CONTEXT --> CONFIG_EL
    MODULE_CONTEXT --> PACKAGES_EL

    INIT --> INIT_PHASE
    CONFIG_EL --> CONFIG_PHASE
    AUTOLOAD --> AUTOLOAD_PHASE
```

## パッケージ管理フロー図

```mermaid
flowchart TB
    subgraph "Package Declaration"
        PKG_MACRO["package! name :recipe"]
        DOOM_PACKAGES[doom-packages<br/>alist]
    end

    subgraph "Package Resolution"
        STRAIGHT_RESOLVE[straight.el]
        LOCKFILE[versions/lockfile]
        PIN[":pin commit"]
    end

    subgraph "Package Sources"
        GITHUB[GitHub]
        MELPA[MELPA]
        GNU_ELPA[GNU ELPA]
        LOCAL[Local Repo]
    end

    subgraph "Build Process"
        CLONE[Git Clone]
        BUILD[Byte Compile]
        NATIVE[Native Compile<br/>Emacs 28+]
        AUTOLOADS[Generate Autoloads]
    end

    subgraph "Runtime"
        LOAD_PATH[load-path]
        AFTER_MACRO["after! macro"]
        LAZY_LOAD[Lazy Loading]
    end

    PKG_MACRO --> DOOM_PACKAGES
    DOOM_PACKAGES --> STRAIGHT_RESOLVE
    LOCKFILE --> STRAIGHT_RESOLVE
    PIN --> STRAIGHT_RESOLVE

    STRAIGHT_RESOLVE --> GITHUB
    STRAIGHT_RESOLVE --> MELPA
    STRAIGHT_RESOLVE --> GNU_ELPA
    STRAIGHT_RESOLVE --> LOCAL

    GITHUB --> CLONE
    MELPA --> CLONE
    GNU_ELPA --> CLONE
    LOCAL --> BUILD

    CLONE --> BUILD
    BUILD --> NATIVE
    BUILD --> AUTOLOADS

    AUTOLOADS --> LOAD_PATH
    LOAD_PATH --> AFTER_MACRO
    AFTER_MACRO --> LAZY_LOAD
```

## CLIコマンド構成図

```mermaid
graph TB
    subgraph "Entry Points"
        BIN_DOOM[bin/doom]
        DOOMSCRIPT[bin/doomscript]
    end

    subgraph "CLI Framework"
        DOOM_CLI_EL[doom-cli.el]
        CLI_COMMANDS[CLI Commands]
    end

    subgraph "Core Commands"
        SYNC[doom sync]
        DOCTOR[doom doctor]
        UPGRADE[doom upgrade]
        ENV[doom env]
        INSTALL[doom install]
    end

    subgraph "Build Commands"
        GC[doom gc]
        BUILD[doom build]
        CLEAN[doom clean]
    end

    subgraph "Development Commands"
        RUN[doom run]
        TEST[doom test]
        PROFILE[doom profile]
    end

    BIN_DOOM --> DOOM_CLI_EL
    DOOMSCRIPT --> DOOM_CLI_EL
    DOOM_CLI_EL --> CLI_COMMANDS

    CLI_COMMANDS --> SYNC
    CLI_COMMANDS --> DOCTOR
    CLI_COMMANDS --> UPGRADE
    CLI_COMMANDS --> ENV
    CLI_COMMANDS --> INSTALL

    CLI_COMMANDS --> GC
    CLI_COMMANDS --> BUILD
    CLI_COMMANDS --> CLEAN

    CLI_COMMANDS --> RUN
    CLI_COMMANDS --> TEST
    CLI_COMMANDS --> PROFILE

    SYNC --> |"Updates"| UPGRADE
    DOCTOR --> |"Diagnoses"| SYNC
```

## データディレクトリ構成図

```mermaid
graph TB
    subgraph "XDG Directories"
        XDG_CONFIG["$XDG_CONFIG_HOME/doom<br/>($DOOMDIR)"]
        XDG_DATA["$XDG_DATA_HOME/doom<br/>(doom-data-dir)"]
        XDG_CACHE["$XDG_CACHE_HOME/doom<br/>(doom-cache-dir)"]
        XDG_STATE["$XDG_STATE_HOME/doom<br/>(doom-state-dir)"]
    end

    subgraph "User Config ($DOOMDIR)"
        USER_INIT[init.el]
        USER_CONFIG[config.el]
        USER_PACKAGES[packages.el]
        USER_CUSTOM[custom.el]
        USER_MODULES[modules/]
    end

    subgraph "Data Directory"
        PROFILES[profiles/]
        PROFILE_DATA["@version/"]
        INIT_COMPILED[init.VERSION.el]
    end

    subgraph "Cache Directory"
        ELN[eln/]
        PCACHE[pcache/]
        COMP[comp/]
        BUILD_CACHE[build cache]
    end

    subgraph "State Directory"
        HISTORY[history]
        RECENT[recentf]
        BOOKMARKS[bookmarks]
        SESSIONS[sessions/]
    end

    subgraph "Local Directory (.local)"
        STRAIGHT_DIR[straight/]
        REPOS[repos/]
        BUILD_DIR["build-VERSION/"]
        VERSIONS[versions/]
    end

    XDG_CONFIG --> USER_INIT
    XDG_CONFIG --> USER_CONFIG
    XDG_CONFIG --> USER_PACKAGES
    XDG_CONFIG --> USER_CUSTOM
    XDG_CONFIG --> USER_MODULES

    XDG_DATA --> PROFILES
    PROFILES --> PROFILE_DATA
    PROFILE_DATA --> INIT_COMPILED

    XDG_CACHE --> ELN
    XDG_CACHE --> PCACHE
    XDG_CACHE --> COMP
    XDG_CACHE --> BUILD_CACHE

    XDG_STATE --> HISTORY
    XDG_STATE --> RECENT
    XDG_STATE --> BOOKMARKS
    XDG_STATE --> SESSIONS

    STRAIGHT_DIR --> REPOS
    STRAIGHT_DIR --> BUILD_DIR
    STRAIGHT_DIR --> VERSIONS
```

## フックシステム構成図

```mermaid
graph TB
    subgraph "Startup Hooks"
        BEFORE_INIT[doom-before-init-hook]
        AFTER_INIT[doom-after-init-hook]
        BEFORE_MOD_INIT[doom-before-modules-init-hook]
        AFTER_MOD_INIT[doom-after-modules-init-hook]
        BEFORE_MOD_CONFIG[doom-before-modules-config-hook]
        AFTER_MOD_CONFIG[doom-after-modules-config-hook]
    end

    subgraph "Runtime Hooks"
        FIRST_INPUT[doom-first-input-hook]
        FIRST_FILE[doom-first-file-hook]
        FIRST_BUFFER[doom-first-buffer-hook]
        SWITCH_BUFFER[doom-switch-buffer-hook]
        SWITCH_WINDOW[doom-switch-window-hook]
        SWITCH_FRAME[doom-switch-frame-hook]
    end

    subgraph "UI Hooks"
        INIT_UI[doom-init-ui-hook]
        LOAD_THEME[doom-load-theme-hook]
    end

    subgraph "Hook Execution"
        DOOM_RUN_HOOKS[doom-run-hooks]
        DOOM_RUN_HOOK[doom-run-hook]
        ERROR_HANDLER[doom-hook-error]
    end

    BEFORE_INIT --> BEFORE_MOD_INIT
    BEFORE_MOD_INIT --> AFTER_MOD_INIT
    AFTER_MOD_INIT --> BEFORE_MOD_CONFIG
    BEFORE_MOD_CONFIG --> AFTER_MOD_CONFIG
    AFTER_MOD_CONFIG --> AFTER_INIT
    AFTER_INIT --> INIT_UI
    INIT_UI --> FIRST_INPUT
    FIRST_INPUT --> FIRST_BUFFER
    FIRST_BUFFER --> FIRST_FILE

    DOOM_RUN_HOOKS --> DOOM_RUN_HOOK
    DOOM_RUN_HOOK --> ERROR_HANDLER
```
