メインコンテンツへスキップ
見出し画像

【AutoCAD】増えすぎたAutoLispを整理・即実行!コマンド名暗記しなくてよい「LispPalette」を無料公開します

    AutoLispをたくさん用意したけど、いちいちボタンを作成するのも大変だし、コマンド名も忘れてしまう…
    それにAutoLispはロードのスタートアップ登録しないといけない…
    これらの作業は割とメンドクサイし、新しいPCに引っ越しもタイヘン

    そこで今回、フォルダ内のLISPを自動解析し、必要なときだけキーボードからサッと呼び出せる専用のコマンドパレット「LispPalette(コマンド: LP)」を紹介します

    LispPaletteでできること(主な特徴)

    CADの「ツールパレット」のように画面の端にずっと出しておくものではないのですが、画面を邪魔しないで、 必要なときだけ LP と打って画面中央に呼び出しして使用します。

    1. フォルダ内の全LISPを一括自動ロード

      • 指定フォルダ(例:C:\AutoLISP\)にあるLISPファイルを、起動時やパレット呼出時にまとめて自動読み込みします。手動で1つずつロードする手間はもうありません。

    2. あいまい検索&カテゴリ絞り込み

      • コマンド名そのものはもちろん、日本語の「ツール表示名」「機能説明」「キーワード」からスペース区切りの絞り込み検索が可能です。

    3. クリック or Enter一発で即実行

      • 候補を選んでクリックするか、Enterキーを押すだけでAutoCAD画面へ戻り、その機能が即座に起動します。

    4. 「★ 最近使った項目」の履歴機能

      • 直近で使ったコマンド(最大15件)が自動で上位や専用カテゴリに記録されます。「いつものツール」を探す手間すら省けます。

    5. コード修正時も安心の自動再ロード

      • パレットから実行するたびに最新のLSPファイルを安全に再読み込みするため、コードを更新した直後でもそのまま反映されます。

    画面イメージと使い方

    画像

    使い方は非常にシンプルです。

    1. コマンドラインに LP と入力してEnter。

    2. 画面中央に専用パレットが起動します。

    3. 探したい機能のキーワード(例:「写真」「連番」「枠」など)を入力するか、カテゴリから選択。

    4. 出てきたカードをクリック(またはEnter)するだけでコマンドが実行されます!

    ※ 画面を開かずに全LISPの再ロードだけを行いたいときは、LL または LPLOAD と打つだけで一括更新されます。

    自分好みにカスタマイズする方法

    コードの上部に、誰でも簡単に調整できる【設定エリア】を用意しています。 必要に応じて書き換えてみてください。

    ;; ==========================================================================
    ;; 【設定エリア】
    ;; ==========================================================================
    
    ;; 1. 【サポート検索パスの読み込み設定】
    (setq *LP_INCLUDE_SUPPORT_PATHS* nil)
    
    ;; 2. 【検索対象フォルダの設定】
    (setq *LP_SEARCH_FOLDERS*
      (list
        "C:\\AutoLisp\\"
        ;; "D:\\MyLispTools\\"
        ;; "Z:\\社内共有\\AutoLISP\\"
      )
    )
    
    ;; 3. 【最近使ったコマンドの履歴ファイル保存先】
    ;; nil の場合はAutoCAD標準の一時フォルダ (TEMPPREFIX) に保存されます
    (setq *LP_HISTORY_FILE* "C:\\AutoLisp\\.lp_history.txt")
    
    ;; 4. 【LISPファイルの一括自動ロード設定】
    ;; T: ロードする / nil: ロードしない
    (setq *LP_LOAD_ON_STARTUP* T)   ;; LispPalette.lsp 読み込み時(apploadスタートアップ時)に全LISPを一括ロード
    (setq *LP_LOAD_ON_LP_OPEN* T) ;; LPコマンド起動時にも毎回全LISPを再ロードする場合は T
    ;; 5. 【パレットの表示設定】
    ;; 文字サイズ(px): 基準となる文字の大きさ。全体がこれに比例して拡大縮小します (9〜28 / 標準 13)
    (setq *LP_FONT_SIZE* 8)
    ;; ウィンドウの幅・高さ(px): (幅 400〜2000 / 標準 620) (高さ 400〜1600 / 標準 680)
    (setq *LP_WIN_W* 400)
    (setq *LP_WIN_H* 500)
    
    • 社内サーバー共有にも対応: 共有サーバーのパス("Z:\\設計部\\LISP\\")を追加すれば、チーム全員で共通のツールパレットとして使うこともできます。

    お手持ちの自作LISPを綺麗に表示させるコツ

    LispPaletteは、LISPファイルの先頭にあるコメントヘッダーを自動で読み取って画面を構成します。 ご自身で作ったLISPの先頭に、以下のようなヘッダーを追記してみてください。

    Lisp

    ;; ==========================================================================
    ;; @Category: 寸法・旗上げ, 土木・測量
    ;; @DisplayName: 写真撮影方向インジケーター
    ;; @Keywords: 写真 撮影 位置 方向 旗上げ 引出線 引き出し 丸矢印 IND
    ;; @Description: 写真撮影方向を示す番号・記号入り丸矢印(引出線)を連続作図します
    ;; ==========================================================================
    (defun c:IND ()
      ...
    
    • @Category: カンマ区切りで複数カテゴリを設定可能。

    • @DisplayName: パレット上に大きく太字で表示される日本語名。

    • @Keywords: 検索にヒットさせたい表記ゆれ単語(ひらがな、カタカナ、同義語など)。

    • @Description: 機能の簡単な説明文。

    ※このヘッダーが書かれていない一般的なLISPでも、ファイル内の c:コマンド名 を自動検出して「未分類」としてリストアップしてくれます。

    LispPaletteのコードはこちら

    以下よりダウンロードしてください。 (保存場所は C:\AutoLisp\ がおすすめです)

    utoCADへの読み込み方法(APPLOADのスタートアップ登録など)が分からない方は、こちらの基本手順記事も併せてご覧ください。

    ;; ==========================================================================
    ;; @Category: システム・管理, ツール
    ;; @DisplayName: LispPalette (コマンドランチャー)
    ;; @Keywords: ランチャー パレット コマンド 検索 ツール 一覧 管理 LP KW HTA
    ;; @Description: 指定フォルダ内のLISPを解析し、大きな文字で検索・即座に実行するランチャー
    ;; ==========================================================================
    
    (vl-load-com)
    
    ;; ==========================================================================
    ;; 【設定エリア】
    ;; ==========================================================================
    
    ;; 1. 【サポート検索パスの読み込み設定】
    (setq *LP_INCLUDE_SUPPORT_PATHS* nil)
    
    ;; 2. 【検索対象フォルダの設定】
    (setq *LP_SEARCH_FOLDERS*
      (list
        "C:\\AutoLisp\\"
        ;; "D:\\MyLispTools\\"
        ;; "Z:\\社内共有\\AutoLISP\\"
      )
    )
    
    ;; 3. 【最近使ったコマンドの履歴ファイル保存先】
    ;; nil の場合はAutoCAD標準の一時フォルダ (TEMPPREFIX) に保存されます
    (setq *LP_HISTORY_FILE* "C:\\AutoLisp\\.lp_history.txt")
    
    ;; 4. 【LISPファイルの一括自動ロード設定】
    ;; T: ロードする / nil: ロードしない
    (setq *LP_LOAD_ON_STARTUP* T)   ;; LispPalette.lsp 読み込み時(apploadスタートアップ時)に全LISPを一括ロード
    (setq *LP_LOAD_ON_LP_OPEN* T) ;; LPコマンド起動時にも毎回全LISPを再ロードする場合は T
    ;; 5. 【パレットの表示設定】
    ;; 文字サイズ(px): 基準となる文字の大きさ。全体がこれに比例して拡大縮小します (9〜28 / 標準 13)
    (setq *LP_FONT_SIZE* 8)
    ;; ウィンドウの幅・高さ(px): (幅 400〜2000 / 標準 620) (高さ 400〜1600 / 標準 680)
    (setq *LP_WIN_W* 400)
    (setq *LP_WIN_H* 500)
    
    ;; ==========================================================================
    ;; フォルダ内LISP一括ロード関数 (loader.lsp代替機能)
    ;; ==========================================================================
    (defun lp_load_all_lisps (/ target_folders sp fld files fname lsp_path count err_cnt res)
      (setq target_folders nil count 0 err_cnt 0)
    
      (if (and *LP_SEARCH_FOLDERS* (listp *LP_SEARCH_FOLDERS*))
        (foreach fld *LP_SEARCH_FOLDERS*
          (if (and fld (/= fld "") (vl-file-directory-p fld))
            (progn
              (if (not (member (substr fld (strlen fld)) '("/" "\\")))
                (setq fld (strcat fld "\\"))
              )
              (if (not (member fld target_folders))
                (setq target_folders (append target_folders (list fld)))
              )
            )
          )
        )
      )
    
      (if *LP_INCLUDE_SUPPORT_PATHS*
        (foreach sp (lp_get_support_paths)
          (if (not (member sp target_folders))
            (setq target_folders (append target_folders (list sp)))
          )
        )
      )
    
      (foreach fld target_folders
        (setq files (vl-directory-files fld "*.lsp" 1))
        (foreach fname files
          ;; 自分自身、および廃止されたLOADER.LSPは除外
          (if (not (or (wcmatch (strcase fname) "LISPPALETTE*")
                       (= (strcase fname) "LOADER.LSP")))
            (progn
              (setq lsp_path (strcat fld fname))
              (setq res (vl-catch-all-apply 'load (list lsp_path "FAILED")))
              (if (or (vl-catch-all-error-p res) (= res "FAILED"))
                (progn
                  (setq err_cnt (1+ err_cnt))
                  (princ (strcat "\n[LispPalette] ロード失敗: " fname))
                )
                (setq count (1+ count))
              )
            )
          )
        )
      )
      (princ (strcat "\n[LispPalette] 計 " (itoa count) " 個のLISPファイルを一括ロードしました"
                     (if (> err_cnt 0) (strcat " (エラー: " (itoa err_cnt) " 個)") "")
                     "。"))
      (princ)
    )
    
    ;; 手動実行用コマンド
    (defun c:LPLOAD () (lp_load_all_lisps) (princ))
    (defun c:LL () (c:LPLOAD))
    
    ;; --- 起動コマンド ---
    (defun c:LP ()
      (if *LP_LOAD_ON_LP_OPEN*
        (lp_load_all_lisps)
      )
      (launch_lisp_palette_modal)
      (princ)
    )
    
    ;; ==========================================================================
    ;; メイン処理
    ;; ==========================================================================
    (defun launch_lisp_palette_modal (/ target_folders support_paths folder files 
                                         all_data categories temp_hta temp_res f res_f fname 
                                         lsp_file fl lne c_cat_raw c_cat_list c_disp c_kws 
                                         c_desc pos temp_sub space_pos found_name base_cmd 
                                         new_all wsh res_cmd run_cmd file_path
                                         hist_file hist_list hl line_item lp_fs lp_w lp_h)
    
      ;; 表示設定の正規化 (数値でない場合や範囲外は標準値/範囲内に補正)
      (setq lp_fs (if (numberp *LP_FONT_SIZE*) (fix *LP_FONT_SIZE*) 13))
      (setq lp_fs (max 9 (min 28 lp_fs)))
      (setq lp_w (if (numberp *LP_WIN_W*) (fix *LP_WIN_W*) 620))
      (setq lp_w (max 400 (min 2000 lp_w)))
      (setq lp_h (if (numberp *LP_WIN_H*) (fix *LP_WIN_H*) 680))
      (setq lp_h (max 400 (min 1600 lp_h)))
    
      ;; 1. 対象フォルダリストの構築
      (setq target_folders nil)
    
      (if (and *LP_SEARCH_FOLDERS* (listp *LP_SEARCH_FOLDERS*))
        (foreach fld *LP_SEARCH_FOLDERS*
          (if (and fld (/= fld "") (vl-file-directory-p fld))
            (progn
              (if (not (member (substr fld (strlen fld)) '("/" "\\")))
                (setq fld (strcat fld "\\"))
              )
              (if (not (member fld target_folders))
                (setq target_folders (append target_folders (list fld)))
              )
            )
          )
        )
      )
    
      (if *LP_INCLUDE_SUPPORT_PATHS*
        (progn
          (setq support_paths (lp_get_support_paths))
          (foreach sp support_paths
            (if (not (member sp target_folders))
              (setq target_folders (append target_folders (list sp)))
            )
          )
        )
      )
    
      (if (null target_folders)
        (progn
          (alert "検索対象フォルダが設定されていないか、実在しません。\nコード上部の【設定エリア】を確認してください。")
          (exit)
        )
      )
    
      ;; 2. ファイル解析
      (setq all_data nil)
    
      (foreach folder target_folders
        (setq files (vl-directory-files folder "*.lsp" 1))
        (foreach fname files
          (if (not (or (wcmatch (strcase fname) "LISPPALETTE*")
                       (= (strcase fname) "LOADER.LSP")))
            (progn
              (setq lsp_file (strcat folder fname))
              (if (setq fl (open lsp_file "r"))
                (progn
                  (setq c_cat_raw "未分類" c_disp "" c_kws "" c_desc "")
                  (while (setq lne (read-line fl))
                    (setq lne (vl-string-trim " \t\r\n" lne))
                    (cond
                      ((= (substr lne 1 1) ";")
                       (cond
                         ((vl-string-search "@Category:" lne)
                          (setq c_cat_raw (vl-string-trim "; \t" (vl-string-subst "" "@Category:" lne))))
                         ((vl-string-search "@DisplayName:" lne)
                          (setq c_disp (vl-string-trim "; \t" (vl-string-subst "" "@DisplayName:" lne))))
                         ((vl-string-search "@Keywords:" lne)
                          (setq c_kws (vl-string-trim "; \t" (vl-string-subst "" "@Keywords:" lne))))
                         ((vl-string-search "@Description:" lne)
                          (setq c_desc (vl-string-trim "; \t" (vl-string-subst "" "@Description:" lne))))
                       )
                      )
                      ((vl-string-search "DEFUN C:" (strcase lne))
                       (setq pos (vl-string-search "C:" (strcase lne)))
                       (setq temp_sub (substr lne (+ pos 3)))
                       (setq space_pos (vl-string-search " " temp_sub))
                       (setq found_name (vl-string-trim " ()" (substr temp_sub 1 (if space_pos space_pos (strlen temp_sub)))))
    
                       (if (and (> (strlen found_name) 1) (not (wcmatch found_name "*[\"\\']*")))
                         (progn
                           (setq c_cat_list (lp_modal_parse_categories c_cat_raw))
                           (if (and (vl-string-search "(C:" (strcase lne))
                                    (vl-string-search ")" (substr lne (vl-string-search "(C:" (strcase lne)))))
                             (progn
                               (setq base_cmd (substr lne (+ (vl-string-search "(C:" (strcase lne)) 4)))
                               (setq base_cmd (vl-string-trim " ()\"" (substr base_cmd 1 (vl-string-search ")" base_cmd))))
                               (if (> (strlen base_cmd) 0)
                                 (progn
                                   (setq new_all nil)
                                   (foreach item all_data
                                     (if (and (= (strcase (nth 1 item)) (strcase base_cmd)) (= (nth 5 item) lsp_file))
                                       (setq new_all (cons (list (nth 0 item) (nth 1 item) (nth 2 item) 
                                                                 (nth 3 item) (nth 4 item) (nth 5 item) found_name) new_all))
                                       (setq new_all (cons item new_all))
                                     )
                                   )
                                   (setq all_data new_all)
                                 )
                               )
                             )
                             (progn
                               (setq all_data (cons (list c_cat_list found_name (if (/= c_disp "") c_disp found_name) 
                                                          c_kws c_desc lsp_file "") all_data))
                               (setq c_disp "" c_kws "" c_desc "")
                             )
                           )
                         )
                       )
                      )
                    )
                  )
                  (close fl)
                )
              )
            )
          )
        )
      )
    
      (if (null all_data)
        (progn
          (alert "対象フォルダ内に解析可能なLISPファイルが見つかりませんでした。")
          (exit)
        )
      )
    
      (setq all_data (vl-sort all_data '(lambda (a b) (< (strcase (nth 2 a)) (strcase (nth 2 b))))))
    
      ;; 履歴ファイルの読み込み (最大15件)
      (setq hist_file (if (and *LP_HISTORY_FILE* (/= *LP_HISTORY_FILE* ""))
                        *LP_HISTORY_FILE*
                        (strcat (getvar "TEMPPREFIX") "lp_history.txt")))
      (setq hist_list nil)
      (if (findfile hist_file)
        (progn
          (setq hl (open hist_file "r"))
          (if hl
            (progn
              (while (setq line_item (read-line hl))
                (setq line_item (vl-string-trim " \t\r\n" line_item))
                (if (and (/= line_item "") (not (member (strcase line_item) hist_list)))
                  (setq hist_list (append hist_list (list (strcase line_item))))
                )
              )
              (close hl)
            )
          )
        )
      )
    
      (setq categories nil)
      (foreach item all_data
        (foreach cat (car item)
          (if (not (member cat categories)) (setq categories (cons cat categories)))
        )
      )
      (setq categories (reverse categories))
      (if hist_list
        (setq categories (cons "★ 最近使った項目" categories))
      )
      (setq categories (cons "すべて" categories))
    
      ;; 3. HTAファイルおよび結果受け渡しファイル作成
      (setq temp_hta (strcat (getvar "TEMPPREFIX") "lp_app.hta"))
      (setq temp_res (strcat (getvar "TEMPPREFIX") "lp_res.txt"))
      
      (setq f (open temp_hta "w"))
      (write-line "<!DOCTYPE html>" f)
      (write-line "<html><head>" f)
      (write-line "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">" f)
      (write-line "<meta charset=\"Shift_JIS\">" f)
      (write-line "<title>LispPalette</title>" f)
      (write-line "<hta:application id=\"app\" applicationname=\"LispPalette\" border=\"thin\" caption=\"yes\" showintaskbar=\"yes\" singleinstance=\"yes\" sysmenu=\"yes\" windowstate=\"normal\">" f)
      (write-line "<style>" f)
      (write-line "  * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Meiryo', 'Segoe UI', sans-serif; }" f)
      (write-line (strcat "  html, body { width: 100%; height: 100%; overflow: hidden; background: #f0f2f5; font-size: " (itoa lp_fs) "px; }") f)
      (write-line "  #header {" f)
      (write-line "    position: absolute; top: 0; left: 0; width: 100%; height: 6.5em;" f)
      (write-line "    background: #ffffff; padding: 8px 12px; border-bottom: 2px solid #ccd0d5; z-index: 10;" f)
      (write-line "  }" f)
      (write-line "  .ctrl-table { width: 100%; margin-top: 6px; border-collapse: collapse; }" f)
      (write-line "  .ctrl-table td { padding: 0; vertical-align: middle; }" f)
      (write-line "  select { font-size: 1.08em; padding: 6px; width: 96%; border: 1px solid #aaa; border-radius: 4px; outline: none; }" f)
      (write-line "  input { font-size: 1.08em; padding: 6px 10px; width: 96%; border: 1px solid #aaa; border-radius: 4px; outline: none; }" f)
      (write-line "  .btn-sort {" f)
      (write-line "    font-size: 1em; font-weight: bold; padding: 6px 4px; width: 100%;" f)
      (write-line "    background: #e7f3ff; color: #1877f2; border: 1px solid #1877f2; border-radius: 4px;" f)
      (write-line "    cursor: pointer; text-align: center; white-space: nowrap;" f)
      (write-line "  }" f)
      (write-line "  .btn-sort:hover { background: #1877f2; color: #ffffff; }" f)
      (write-line "  #box {" f)
      (write-line "    position: absolute; top: 6.5em; bottom: 2.3em; left: 0; width: 100%;" f)
      (write-line "    overflow-y: scroll; padding: 12px 14px;" f)
      (write-line "  }" f)
      ;; カテゴリ見出しバースタイル(文字化けしない安全な左ボーダー付きデザイン)
      (write-line "  .cat-header {" f)
      (write-line "    font-size: 1em; font-weight: bold; color: #1877f2; background: #e7f3ff;" f)
      (write-line "    padding: 5px 10px; margin: 12px 0 6px 0; border-radius: 4px;" f)
      (write-line "    border-left: 4px solid #1877f2; letter-spacing: 0.5px;" f)
      (write-line "  }" f)
      (write-line "  .cat-header-recent {" f)
      (write-line "    color: #e67e22; background: #fdf5e6; border-left: 4px solid #e67e22;" f)
      (write-line "  }" f)
      (write-line "  .cat-header:first-child { margin-top: 0; }" f)
      (write-line "  .card {" f)
      (write-line "    background: #ffffff; border: 1px solid #ccd0d5; border-radius: 6px;" f)
      (write-line "    padding: 10px 14px; margin-bottom: 8px; cursor: pointer; transition: background 0.15s;" f)
      (write-line "  }" f)
      (write-line "  .card:hover { background: #e7f3ff; border-color: #1877f2; }" f)
      (write-line "  .badge { background: #1877f2; color: #ffffff; font-size: 1em; font-weight: bold; padding: 2px 7px; border-radius: 3px; margin-right: 8px; display: inline-block; }" f)
      (write-line "  .badge-none { background: #8a8d91; }" f)
      (write-line "  .name { font-size: 1.23em; font-weight: bold; color: #1c1e21; }" f)
      (write-line "  .cmd { font-size: 1em; color: #65676b; float: right; font-family: Consolas, monospace; line-height: 1.85em; }" f)
      (write-line "  .desc { font-size: 1em; color: #4b4f56; margin-top: 5px; line-height: 1.4; clear: both; }" f)
      (write-line "  #footer {" f)
      (write-line "    position: absolute; bottom: 0; left: 0; width: 100%; height: 2.3em;" f)
      (write-line "    background: #e4e6eb; text-align: center; line-height: 2.5em; font-size: 0.92em; color: #65676b; border-top: 1px solid #ccd0d5;" f)
      (write-line "  }" f)
      (write-line "</style></head><body>" f)
      
      (write-line "<script>" f)
      (write-line (strcat "window.resizeTo(" (itoa lp_w) ", " (itoa lp_h) ");") f)
      (write-line (strcat "window.moveTo((screen.width-" (itoa lp_w) ")/2, (screen.height-" (itoa lp_h) ")/2);") f)
      (write-line "</script>" f)
    
      (write-line "<div id=\"header\">" f)
      (write-line "  <div style=\"font-size: 1.3em; font-weight: bold; color: #1877f2;\">LispPalette</div>" f)
      (write-line "  <table class=\"ctrl-table\"><tr>" f)
      (write-line "    <td style=\"width: 30%;\">" f)
      (write-line "      <select id=\"catSel\" onchange=\"filterAndSort()\">" f)
      (foreach cat categories (write-line (strcat "        <option value=\"" (lp_sanitize_html cat) "\">" (lp_sanitize_html cat) "</option>") f))
      (write-line "      </select>" f)
      (write-line "    </td>" f)
      (write-line "    <td style=\"width: 46%;\">" f)
      (write-line "      <input type=\"text\" id=\"kw\" placeholder=\"キーワード検索...\" onkeyup=\"filterAndSort()\" autofocus>" f)
      (write-line "    </td>" f)
      (write-line "    <td style=\"width: 24%;\">" f)
      ;; 初期ボタン表示を「順: カテゴリ」に設定
      (write-line "      <button class=\"btn-sort\" id=\"sortBtn\" onclick=\"toggleSort()\">順: カテゴリ</button>" f)
      (write-line "    </td>" f)
      (write-line "  </tr></table>" f)
      (write-line "</div>" f)
    
      (write-line "<div id=\"box\">" f)
      (foreach item all_data
        (setq item_cats  (nth 0 item)
              item_cmd   (nth 1 item)
              item_disp  (nth 2 item)
              item_kws   (nth 3 item)
              item_desc  (nth 4 item)
              item_file  (nth 5 item)
              item_alias (nth 6 item))
        (setq run_target (if (and item_alias (/= item_alias "")) item_alias item_cmd))
        (setq h_idx (if hist_list (vl-position (strcase run_target) hist_list) nil))
        (if (and (null h_idx) hist_list)
          (setq h_idx (vl-position (strcase item_cmd) hist_list))
        )
        (setq cat_attr "")
        (foreach c item_cats (setq cat_attr (strcat cat_attr c " ")))
        (if h_idx
          (setq cat_attr (strcat cat_attr "★ 最近使った項目 "))
        )
        (setq search_target (strcase (strcat item_cmd " " item_disp " " item_kws " " item_desc " " item_alias)))
    
        (write-line (strcat "  <div class=\"card\" "
                            "data-cats=\"" (lp_sanitize_html cat_attr) "\" "
                            "data-search=\"" (lp_sanitize_html search_target) "\" "
                            "data-name=\"" (lp_sanitize_html (strcase item_disp)) "\" "
                            "data-firstcat=\"" (lp_sanitize_html (car item_cats)) "\" "
                            "data-cmd=\"" (lp_sanitize_html (strcase run_target)) "\" "
                            "data-recent=\"" (if h_idx (itoa h_idx) "9999") "\" "
                            "onclick=\"run('" (lp_escape_js run_target) "','" (lp_escape_js (vl-string-translate "\\" "/" item_file)) "')\">") f)
        (write-line "    <div>" f)
        (if (and item_alias (/= item_alias ""))
          (write-line (strcat "      <span class=\"badge\">" (lp_sanitize_html (strcase item_alias)) "</span>") f)
          (write-line "      <span class=\"badge badge-none\">-</span>" f)
        )
        (write-line (strcat "      <span class=\"name\">" (lp_sanitize_html item_disp) "</span>") f)
        (write-line (strcat "      <span class=\"cmd\">" (lp_sanitize_html item_cmd) "</span>") f)
        (write-line "    </div>" f)
        (write-line (strcat "    <div class=\"desc\">" (lp_sanitize_html item_desc) "</div>") f)
        (write-line "  </div>" f)
      )
      (write-line "</div>" f)
      (write-line "<div id=\"footer\">クリックまたはEnterキーで即座に実行</div>" f)
    
      (write-line "<script>" f)
      (write-line (strcat "var resPath = \"" (lp_escape_js (vl-string-translate "\\" "/" temp_res)) "\";") f)
      (write-line "function run(cmd, file) {" f)
      (write-line "  try {" f)
      (write-line "    var fso = new ActiveXObject('Scripting.FileSystemObject');" f)
      (write-line "    var tf = fso.CreateTextFile(resPath, true, false);" f)
      (write-line "    tf.WriteLine(cmd + '|' + file);" f)
      (write-line "    tf.Close();" f)
      (write-line "  } catch(e) {}" f)
      (write-line "  window.close();" f)
      (write-line "}" f)
    
      ;; 初期ソートモードを 1: カテゴリ順 に設定 (1: カテゴリ -> 2: コマンド名 -> 0: 名前順)
      (write-line "var sortMode = 1;" f)
      (write-line "var sortLabels = ['順: 名前順', '順: カテゴリ', '順: コマンド'];" f)
      (write-line "function toggleSort() {" f)
      (write-line "  sortMode = (sortMode === 1) ? 2 : (sortMode === 2) ? 0 : 1;" f)
      (write-line "  document.getElementById('sortBtn').innerText = sortLabels[sortMode];" f)
      (write-line "  filterAndSort();" f)
      (write-line "}" f)
    
      ;; フィルタリング&ソート統合処理
      (write-line "function filterAndSort() {" f)
      (write-line "  var cat = document.getElementById('catSel').value;" f)
      (write-line "  var rawQ = document.getElementById('kw').value.replace(/^\\s+|\\s+$/g, '');" f)
      (write-line "  var q = rawQ.toLowerCase().split(/\\s+/);" f)
      (write-line "  var box = document.getElementById('box');" f)
      (write-line "  var isRecentFilter = (cat === '★ 最近使った項目');" f)
      (write-line "  var isCatMode = (sortMode === 1 && cat === 'すべて' && rawQ === '');" f)
      
      ;; 既存の見出しヘッダーをすべて削除
      (write-line "  var oldHeaders = box.getElementsByClassName('cat-header');" f)
      (write-line "  while (oldHeaders.length > 0) { oldHeaders[0].parentNode.removeChild(oldHeaders[0]); }" f)
    
      (write-line "  var cards = Array.prototype.slice.call(box.getElementsByClassName('card'));" f)
      
      ;; 1. ソート実行
      (write-line "  cards.sort(function(a, b) {" f)
      (write-line "    if (isRecentFilter) {" f)
      (write-line "      var rA = parseInt(a.getAttribute('data-recent') || '9999', 10);" f)
      (write-line "      var rB = parseInt(b.getAttribute('data-recent') || '9999', 10);" f)
      (write-line "      if (rA !== rB) return rA - rB;" f)
      (write-line "    }" f)
      (write-line "    if (isCatMode) {" f)
      (write-line "      var rA = parseInt(a.getAttribute('data-recent') || '9999', 10);" f)
      (write-line "      var rB = parseInt(b.getAttribute('data-recent') || '9999', 10);" f)
      (write-line "      var aIsRec = (rA < 9999);" f)
      (write-line "      var bIsRec = (rB < 9999);" f)
      (write-line "      if (aIsRec && bIsRec) return rA - rB;" f)
      (write-line "      if (aIsRec && !bIsRec) return -1;" f)
      (write-line "      if (!aIsRec && bIsRec) return 1;" f)
      (write-line "      var cA = a.getAttribute('data-firstcat') || '';" f)
      (write-line "      var cB = b.getAttribute('data-firstcat') || '';" f)
      (write-line "      if (cA !== cB) return cA.localeCompare(cB);" f)
      (write-line "    } else if (sortMode === 1) {" f)
      (write-line "      var cA = a.getAttribute('data-firstcat') || '';" f)
      (write-line "      var cB = b.getAttribute('data-firstcat') || '';" f)
      (write-line "      if (cA !== cB) return cA.localeCompare(cB);" f)
      (write-line "    } else if (sortMode === 2) {" f)
      (write-line "      var kA = a.getAttribute('data-cmd') || '';" f)
      (write-line "      var kB = b.getAttribute('data-cmd') || '';" f)
      (write-line "      if (kA !== kB) return kA.localeCompare(kB);" f)
      (write-line "    }" f)
      (write-line "    var nA = a.getAttribute('data-name') || '';" f)
      (write-line "    var nB = b.getAttribute('data-name') || '';" f)
      (write-line "    return nA.localeCompare(nB);" f)
      (write-line "  });" f)
    
      ;; 2. DOMの並び替え反映 & 見出し挿入 & 絞り込み判定
      (write-line "  var currentGroup = '';" f)
      (write-line "  var hasRecentHeader = false;" f)
      (write-line "  for (var i = 0; i < cards.length; i++) {" f)
      (write-line "    var c = cards[i];" f)
      (write-line "    var cCats = c.getAttribute('data-cats') || '';" f)
      (write-line "    var cFirstCat = c.getAttribute('data-firstcat') || '未分類';" f)
      (write-line "    var cSearch = (c.getAttribute('data-search') || '').toLowerCase();" f)
      (write-line "    var cRecent = parseInt(c.getAttribute('data-recent') || '9999', 10);" f)
      (write-line "    var cMatch = (cat === 'すべて' || cCats.indexOf(cat) !== -1);" f)
      (write-line "    var tMatch = true;" f)
      (write-line "    if (rawQ !== '') {" f)
      (write-line "      for (var j = 0; j < q.length; j++) {" f)
      (write-line "        if (q[j] && cSearch.indexOf(q[j]) === -1) { tMatch = false; break; }" f)
      (write-line "      }" f)
      (write-line "    }" f)
      (write-line "    var isVisible = (cMatch && tMatch);" f)
      (write-line "    c.style.display = isVisible ? 'block' : 'none';" f)
      
      ;; 見出し生成
      (write-line "    if (isVisible) {" f)
      (write-line "      if (isCatMode && cRecent < 9999) {" f)
      (write-line "        if (!hasRecentHeader) {" f)
      (write-line "          hasRecentHeader = true;" f)
      (write-line "          var h = document.createElement('div');" f)
      (write-line "          h.className = 'cat-header cat-header-recent';" f)
      (write-line "          h.innerText = '★ 最近使ったコマンド';" f)
      (write-line "          box.appendChild(h);" f)
      (write-line "        }" f)
      (write-line "      } else if (sortMode === 1) {" f)
      (write-line "        if (cFirstCat !== currentGroup) {" f)
      (write-line "          currentGroup = cFirstCat;" f)
      (write-line "          var h = document.createElement('div');" f)
      (write-line "          h.className = 'cat-header';" f)
      (write-line "          h.innerText = '■ ' + currentGroup;" f)
      (write-line "          box.appendChild(h);" f)
      (write-line "        }" f)
      (write-line "      }" f)
      (write-line "    }" f)
      (write-line "    box.appendChild(c);" f)
      (write-line "  }" f)
      (write-line "}" f)
    
      ;; 初期起動時にソート&見出し生成を即座に実行
      (write-line "window.onload = function() { filterAndSort(); };" f)
    
      (write-line "document.getElementById('kw').onkeydown = function(e) {" f)
      (write-line "  e = e || window.event;" f)
      (write-line "  if (e.keyCode === 13) {" f)
      (write-line "    var cards = document.querySelectorAll('.card');" f)
      (write-line "    for (var i = 0; i < cards.length; i++) {" f)
      (write-line "      if (cards[i].style.display !== 'none') { cards[i].click(); break; }" f)
      (write-line "    }" f)
      (write-line "  }" f)
      (write-line "};" f)
      (write-line "</script></body></html>" f)
      (close f)
    
      ;; 4. HTAを同期待ちで起動
      (setq wsh (vlax-create-object "WScript.Shell"))
      (vlax-invoke-method wsh 'Run (strcat "mshta.exe \"" temp_hta "\"") 1 :vlax-true)
      (vlax-release-object wsh)
    
      ;; 5. 選択結果を読み込み、AutoLISP自身が直接実行 & 履歴ファイル更新
      (setq res_cmd nil)
      (if (findfile temp_res)
        (progn
          (setq res_f (open temp_res "r"))
          (if res_f
            (progn
              (setq res_cmd (read-line res_f))
              (close res_f)
            )
          )
          (vl-file-delete temp_res)
        )
      )
      (vl-file-delete temp_hta)
    
      (if (and res_cmd (/= res_cmd ""))
        (progn
          (setq pos (vl-string-search "|" res_cmd))
          (setq run_cmd (substr res_cmd 1 pos))
          (setq file_path (substr res_cmd (+ pos 2)))
    
          ;; 履歴ファイル (.lp_history.txt) の更新 (先頭に最新コマンド、重複除外、最大15件)
          (setq hist_list (vl-remove (strcase run_cmd) hist_list))
          (setq hist_list (cons (strcase run_cmd) hist_list))
          (if (> (length hist_list) 15)
            (setq hist_list (reverse (cdr (reverse hist_list))))
          )
          (if (setq hl (open hist_file "w"))
            (progn
              (foreach h_item hist_list
                (write-line h_item hl)
              )
              (close hl)
            )
          )
    
          ;; 常に最新のLSPファイルを安全に再読み込みして最新のコードを実行
          (if (findfile file_path)
            (vl-catch-all-apply 'load (list file_path nil))
          )
          (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) (strcat "\033\033" run_cmd " "))
        )
      )
      (princ)
    )
    
    ;; ==========================================================================
    ;; サポート関数群
    ;; ==========================================================================
    
    (defun lp_get_support_paths (/ raw_str paths pos item)
      (setq raw_str (getvar "ACADPREFIX") paths nil)
      (while (setq pos (vl-string-search ";" raw_str))
        (setq item (substr raw_str 1 pos))
        (if (and (/= item "") (vl-file-directory-p item))
          (progn
            (if (not (member (substr item (strlen item)) '("/" "\\")))
              (setq item (strcat item "\\"))
            )
            (if (not (member item paths)) (setq paths (append paths (list item))))
          )
        )
        (setq raw_str (substr raw_str (+ pos 2)))
      )
      (if (and (/= raw_str "") (vl-file-directory-p raw_str))
        (progn
          (if (not (member (substr raw_str (strlen raw_str)) '("/" "\\")))
            (setq raw_str (strcat raw_str "\\"))
          )
          (if (not (member raw_str paths)) (setq paths (append paths (list raw_str))))
        )
      )
      paths
    )
    
    (defun lp_modal_parse_categories (str / res pos item)
      (setq str (vl-string-trim " " (vl-string-translate "、," ",," str)))
      (while (setq pos (vl-string-search "," str))
        (setq item (vl-string-trim " \t" (substr str 1 pos)))
        (if (/= item "") (setq res (cons item res)))
        (setq str (substr str (+ pos 2)))
      )
      (setq item (vl-string-trim " \t" str))
      (if (/= item "") (setq res (cons item res)))
      (if res (reverse res) (list "未分類"))
    )
    
    (defun lp_sanitize_html (str)
      (if (null str) (setq str ""))
      (setq str (vl-string-subst "&amp;" "&" str))
      (setq str (vl-string-subst "&quot;" "\"" str))
      (setq str (vl-string-subst "&#39;" "'" str))
      (setq str (vl-string-subst "&lt;" "<" str))
      (setq str (vl-string-subst "&gt;" ">" str))
      str
    )
    
    (defun lp_escape_js (str)
      (if (null str) (setq str ""))
      (setq str (vl-string-subst "\\\\" "\\" str))
      (setq str (vl-string-subst "\\'" "'" str))
      (setq str (vl-string-subst "\\\"" "\"" str))
      str
    )
    
    ;; 起動時(ロード時)の自動一括ロード判定
    (if *LP_LOAD_ON_STARTUP*
      (lp_load_all_lisps)
    )
    
    (princ "\n[LispPalette] ロード完了 (コマンド: LP / LPLOAD)")
    (princ)
     
     
    Civil Engineer in Japan.

    あなたへのおすすめ