1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lispxmpをactive regionのみで実行できるようにした

Last updated at Posted at 2025-01-30

Emacs Lisp 式の値を; =>というコメントをつけた部分に自動注釈するlispxmpというパッケージがあります。

作者のrubikitch氏のブログに詳しいです。

デフォルトではbuffer全体が対象ですが、regionがactiveなときはregion内でだけ実行するように機能を追加しました。

(defun lispxmp ()
  "Annotate value of lines containing `; =>' in the current buffer.
If a region active, annotate only in the region."
  (interactive)
  (save-restriction
      (when (region-active-p)
        (narrow-to-region (region-beginning) (region-end)))
      (let ((pt (point)) (wstart (window-start (selected-window))))
        (lispxmp-create-code (current-buffer))
        (delete-region (point-min) (point-max))
        (insert-buffer-substring lispxmp-temp-buffer)
        (unwind-protect
            (if debug-on-error
                (eval-buffer)
              (condition-case err
                  (eval-buffer)
                (error
                 (ding)
                 ;; next action when error
                 (run-with-timer 0 nil 'message "Error in eval: %S" err))))
          (lispxmp-create-annotations (current-buffer) lispxmp-results)
          (goto-char pt)
          (set-window-start (selected-window) wstart)))))

以下で公開しています。

他にも

  • clからcl-libへの変更
  • defadviceからadvice-addへの変更

など動作に変更はない部分での、新しいversionへの対応をしています(とはいえこういう対応をしなくても動くのがEmacsの素晴らしいところではありますよね)。

2025-02-08追記
save-excursionでは戻りきれていなかったので、元々の実装にならって修正しました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?