パソコン・情報公開## Emacs-Lispで,タイトルにファイル名を交えた投稿テキストを取得する自作コマンド

:CATEGORIES: #@kanazawabengosi #金沢弁護士会 #@JFBAsns #日本弁護士連合会(日弁連) #法務省 #@MOJ_HOUMU #Emacs #Emacs-Lisp

 Emacs-Lispのプログラミングもけっこうブランクがあったので,最悪,午前中いっぱいは時間を使いそうと思っていたのですが,思いがけずすんなりと行きました。これで告発状作成の作業効率も改善に期待が持ています。

;;;;2017-08-24;;;;
(defun k-markdown-copy-to-subtree ()
  "Narrow buffer to the current subtree."
  (interactive)
  (save-excursion
  (let ((=start)(=end))
       (progn (markdown-back-to-heading-over-code-block t) (setq =start (point)))
       (progn (markdown-end-of-subtree)
	      (if (and (markdown-heading-at-point) (not (eobp)))
		  (backward-char 1))
	      (setq =end (point))(copy-region-as-kill =start =end)))))

;;2021-02-04
(defun k-markdown-copy-to-subtree ()
  "Narrow buffer to the current subtree."
  (interactive)
  (save-excursion
  (let ((=start)(=end))
       (progn (markdown-back-to-heading-over-code-block t) (setq =start (point)))
       (progn (markdown-end-of-subtree)
	      (if (and (markdown-heading-at-point) (not (eobp)))
		  (backward-char 1))
	      (setq =end (point))
	      (setq fname (file-name-nondirectory (buffer-file-name)))
	      (string-match ".+_\\(.+\\)\\.md" fname)
	      (setq fname (match-string 1 fname))
	      ;;(copy-region-as-kill =start =end)
	      ;; 指定した範囲のリージョンのテキストを変数に代入
	      (setq text (concat fname (buffer-substring-no-properties =start =end)))
	      (message fname)
	      ;; クリップボードに変数の内容を送る。
	      (x-set-selection nil text)
	      ))))

 同名の関数ですが,後のものが上書きされて有効になります。以前のものは2017年8月24日となっていました。今回,追加した機能は,ファイル名の一部を見出しの先頭に追加するという処理です。

 例えば,「2020-12-11-152316_深澤諭史弁護士(第二東京弁護士会).md」というのがファイル名で,「深澤諭史弁護士(第二東京弁護士会)」がテキストの抽出部分になります。正規表現を使っていますが,Emacs-Lispだとエスケープの数が増えます。

 テキストの取得は,「markdown-end-of-subtree」などというmarkdown-modeにある関数を呼び出して使っています。だいたいはキーマップに登録されているので,Emacsの場合,キーバインドから関数名と編集位置を簡単に探し出すことが出来ます。