0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Emacs LispAdvent Calendar 2024

Day 10

Emacs Lisp 超初心者がコマンドを作ってみる 〜 そもそも動かない-括弧対応ミスUnbalanced parentheses 〜

Posted at

はじめに

まずは、そもそもシンタックスエラーで動かない時には

End of file during parsing

と素っ気ないメッセージが出て

で、これの詳細を見ようと C-M-x eval-defun すると
一番よく拝むハメになるのが

Scan error: "Unbalanced parentheses", 1027, 2603

という、「括弧の対応が合ってない」というエラーなのだけど、
これの数字の意味を知らなかった。 桁位置,行位置って訳でも無いしな?

Scan error: "Unbalanced parentheses" の数字の意味

ポイントだそう。

指定のポイントに移動するには

M-g c goto-char を使う。

今回は 1027 は (defun の先頭の括弧で、 2603 はバッファ末尾だった。

約に立たねぇ! だいたいおおまかにそこらへんの範囲でズレてるだろって事だそうである。

括弧対応間違いの探し方

探し方は、公式には
こちらさんを参考にしてください。
19.3 無効なLisp構文のデバッグ
https://ayatakesi.github.io/lispref/29.4/elisp-ja.html#Syntax-Errors

個人的には Emacs など Elisp-mode を備えたエディタで括弧の対応を見つつ、 特にクローズ側はまとめて書いてしまわずにそれぞれ1行ずつとって閉じるように書く

ありがちな書き方。とにかく閉じ括弧を纏めてしまう。

(defun po-ediff-previous-msgid ()
  "Ediff previous msgid (marked #| ) and msgid."
  (interactive)
  (require 'po-mode)
  (po-find-span-of-entry)
  (let ((oldbuf (current-buffer))
	(msgid (po-get-msgid))
	(untranslated-regions (po-previous-untranslated-regions)))
    ;; source buffer for buffer-A
    (save-current-buffer
      (set-buffer (get-buffer-create
		   po-ediff-previous-msgid-buffer-a-name))
      (dolist (region untranslated-regions)
	(insert (po-extract-previous-msgid oldbuf (car region) (cdr region))))
      (restore-buffer-modified-p nil))
    ;; source buffer for buffer-B
    (save-current-buffer
      (set-buffer (get-buffer-create
		   po-ediff-previous-msgid-buffer-b-name))
      (insert msgid)
      (restore-buffer-modified-p nil)))
  )

個人的にやってる書き方。行数増えちゃいますけど、括弧の対応を重視

(defun po-ediff-previous-msgid ()
  "Ediff previous msgid (marked #| ) and msgid."
  (interactive)
  (require 'po-mode)
  (po-find-span-of-entry)
  (let (
	(oldbuf (current-buffer))
	(msgid (po-get-msgid))
	(untranslated-regions (po-previous-untranslated-regions))
        (beg-A)
	(end-A)
	(beg-B)
	(end-B)
	)
    ;; source buffer for buffer-A
    (save-current-buffer
      (set-buffer (get-buffer-create
		   po-ediff-previous-msgid-buffer-a-name))
      (setq buffer-read-only nil)
      (erase-buffer)
      (dolist (region untranslated-regions)
	(insert (po-extract-previous-msgid oldbuf (car region) (cdr region)))
	)
      (setq beg-A (point-min))
      (setq end-A (point-max))
      (goto-char (point-min))
      (push-mark (point-max) t t)
      (setq buffer-read-only t)
      (restore-buffer-modified-p nil))
    ;; source buffer for buffer-B
    (save-current-buffer
      (set-buffer (get-buffer-create
		   po-ediff-previous-msgid-buffer-b-name))
      (setq buffer-read-only nil)
      (erase-buffer)
      (insert msgid)
      (setq beg-B (point-min))
      (setq end-B (point-max))
      (goto-char (point-min))
      (push-mark (point-max) t t)
      (setq buffer-read-only t)
      (restore-buffer-modified-p nil))
    ;; run ediff
    (ediff-regions-internal
     (get-buffer po-ediff-previous-msgid-buffer-a-name)
     beg-A end-A
     (get-buffer po-ediff-previous-msgid-buffer-b-name)
     beg-B end-B
     nil 'ediff-regions-wordwise 'word-mode nil)
    )  ; end of let
  )
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?