Mercurial > hg > RemoteEditor > emacs
changeset 52:769ebe87cd5a
指定した行がバッファの末尾行を超えている場合、
その足りない数だけ改行を入れるように修正。
author | gongo |
---|---|
date | Tue, 26 Aug 2008 15:19:34 +0900 |
parents | 571a48fae227 |
children | 0b7ba6b2a0f9 |
files | test/buffer-edit/insert-line-to-buffer.el |
diffstat | 1 files changed, 36 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/test/buffer-edit/insert-line-to-buffer.el Mon Aug 25 21:14:00 2008 +0900 +++ b/test/buffer-edit/insert-line-to-buffer.el Tue Aug 26 15:19:34 2008 +0900 @@ -1,16 +1,39 @@ -;; -;; insert-line-to-buffer.el ... 指定したバッファの、指定した行に、指定した文字列を挿入。 -;; buf : 編集するバッファ +;; +;; insert-line-to-buffer.el +;; 指定したバッファの、指定した行に、指定した文字列を挿入。 +;; バッファの最後尾行が指定した行に足りない場合、その数だけ改行する +;; +;; buffer : 編集するバッファ(名) ;; lineno : 編集するbufの行番号 -;; str : lineno行目に挿入する文字列 +;; string : lineno行目に挿入する文字列 +(defun insert-line-to-buffer (buffer lineno string) + "Insert STRING at line LINENO of BUFFER. +The line LINENO is deleted, and STRING is inserted." + (let (curlineno) + (save-excursion + (set-buffer (get-buffer-create buffer)) + (goto-line lineno) ;; 指定行番号へ移動 + (setq curlineno (real-count-lines (point))) ;; 現在の行番号 -(defun insert-line-to-buffer (buf lineno str) - (let ((oldbuf (current-buffer))) - (save-current-buffer - (set-buffer (get-buffer-create buf)) - (goto-line lineno) ;;指定の行位置へ移動 - ;; 行頭から末尾までのテキストを削除 - (delete-region (progn (beginning-of-line) (point)) - (progn (end-of-line) (point))) + (if (> lineno curlineno) + ;; buffer の 最後の行番号が、指定した lineno に足りない場合、 + ;; その行数だけ改行し、その行へ移動する。 + ;; newline なので、下のようにテキストを削除する必要は無い + (progn (end-of-line) + (newline (- lineno curlineno)) + (goto-line lineno)) + + ;; 行頭から末尾までのテキストを削除 + (delete-region (progn (beginning-of-line) (point)) + (progn (end-of-line) (point))) + ) + ;; 新しい行を挿入 - (insert str)))) \ No newline at end of file + (insert string)))) + +;; currentp の位置の行番号を返す +(defun real-count-lines (currentp) + (+ (count-lines (point-min) currentp) + (if (= (save-excursion (goto-char currentp) (current-column)) + 0) + 1 0))) \ No newline at end of file