1. 연구 모듈/Emacs
[Emacs/Lisp] 특정 문자열 대체 관련 함수 정의
사용자-1
2019. 8. 6. 07:30
- my-rewind : 버퍼 내에 맨 앞으로 이동하는 함수
- my-replace : 현재 위치로부터 뒤로 검색하면서 특정 문자열을 다른 문자열로 모두 바꿔주는 함수 (bound: 특정 위치까지 검색. nil이면 버퍼끝까지)
- my-replace-1t : 현재 위치로부터 뒤로 검색하면서 특정 문자열을 다른 문자열로 1회 바꿔주는 함수. 대체가 일어났을 경우 t를, 아니면 nil을 리턴 (bound: 특정 위치까지 검색. nil이면 버퍼끝까지)
함수 정의
(defun my-rewind ()
(goto-char (point-min))
)
(defun my-replace (str-from str-to bound)
(progn
(while (search-forward str-from bound t)
(progn
(delete-backward-char (length str-from))
(insert str-to)
)
)
)
)
(defun my-replace-1t (str-from str-to bound)
(progn
(if (search-forward str-from bound t)
(progn
(delete-backward-char (length str-from))
(insert str-to)
t
)
nil
)
)
)
사용 예
(defun my-replace-example () (interactive) (my-rewind) (my-replace "abc" "def" nil) ;버퍼내에 모든 'abc'를 'def'로 바꿈 )