mieki256's diary



2026/03/06(金) [n年前の日記]

#1 [xyzzy] xyzzyの設定を見直し中

テキストエディタ xyzzy用に _nim-mode.l を作って追加したけれど、ついでに設定ファイル ~/.xyzzy の内容を見直し中。

関数を追加 :

せっかくだから、ここ最近欲しいなと思っていた関数を ~/.xyzzy に追加した。AI(Google Gemini)に作成してもらった。

セレクション(選択範囲)の上下に入力文字列を追加する。tag - /tag という形でセレクションを挟み込む。

;; ----------------------------------------
;; セレクションの直前と直後に「tag」「/tag」を挿入

(defun enclose-selection-with-tag ()
  "選択範囲の直前と直後に tag /tag を挿入。"
  (interactive) ; 引数を取らず、関数内で入力を制御
  (or (pre-selection-p)
      (error "Selection(選択範囲)がありません。(;_;)"))

  ;; read-string で入力を取得
  (let ((tag (read-string "Insrt Tag name: " :default "PRE")))
    (when (and tag (not (string= tag "")))
      (let ((from (selection-mark))
            (to (selection-point)))

        ;; 選択方向(上下)に関わらず from を小さく、to を大きく補正
        (if (> from to)
            (rotatef from to))

        (save-excursion
          ;; 選択範囲の後ろから挿入(位置ズレ防止)
          (goto-char to)
          (insert "/" tag "\n")

          ;; 選択範囲の前に挿入
          (goto-char from)
          (insert tag "\n"))))))

;; C-z m に割り当て
(global-set-key '(#\C-z #\m) 'enclose-selection-with-tag)

このぐらいの長さの関数なら、AIと数回やり取りするだけでAIが作ってくれる。凄い時代になった…。

ただ、ちょっと突っ込んだ質問をAIにすると、おそらくは Emacs関連の情報をそれっぽく織り交ぜてガンガン嘘をつき始める…。Emacs も xyzzy も、どちらも Lisp で機能を拡張できるけど、Emacs は Emacs Lisp、xyzzy は Common Lisp で、API?も色々違うんだよなあ…。マイナーな言語、マイナーなアプリはツラい。

別の関数も追加 :

セレクション(選択範囲)の行頭から入力した文字列を削除する関数も追加してみた。

outcomment-selection は 以下のページで紹介されてる関数をそっくりそのまま利用させてもらった。

_xyzzy の音 - 編集

;; ----------------------------------------
;; セレクションの指定行頭文字を削除

(defun outcomment-selection (key)
  (or (pre-selection-p)
      (error "selectionがない。"))
  (let ((from (selection-mark))
        (to (selection-point)))
    (if (> from to)
        (rotatef from to))
    (save-excursion
      (save-restriction
        (narrow-to-region from to)
        (goto-char from)
        (replace-buffer (concat "^" key) "" :regexp t)))))

(defun my-ask-and-outcomment (key)
  "ミニバッファで入力された文字列を選択範囲の行頭から削除します。"
  (interactive "sKey to remove: ") ; "s"は文字列(string)を入力させる指定
  (outcomment-selection key))

;; C-z e に割り当て
(global-set-key '(#\C-z #\e) 'my-ask-and-outcomment)

ミニバッファ上で文字列を入力する際に、interactive を使う方法と、read-string を使う方法があるようだけど…。後者はデフォルト値を設定できるあたりが便利なのだろうか? というわけでもないな…。interactive も初期値を指定することができそう。関数を実行して即座に入力させるか、何か処理してから入力させるか、呼び出すタイミングを変更できるかどうかが違うのだろうか。

_interactive - 関数

以上です。

過去ログ表示

Prev - 2026/03 -
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project