emacs 에서 동시에 .h 와 .cpp 에디팅

jinserk의 이미지

emacs 를 써볼까 하고 .emacs 파일을 편집하다 보니 점점 더 IDE 에 욕심이 나네요.

제가 원하는 것은 emacs 를 띄울때 처음부터 화면이 좌우로 나뉘고, 왼쪽에는 .h 파일이, 오른쪽에는 .cpp 파일이 뜨도록 하는 것입니다.
새로운 파일을 열때마다 그 파일이 .c, .cc, .cpp 이면 .h 가 왼쪽에 뜨고, .h 파일이면 오른쪽에 뜨도록 말이죠.

보통 제가 코딩할때 cpp 와 h 를 한쌍으로 만들다보니 이런 욕심이 나네요.

어떤식으로 .el 을 만들면 될까요. 한수 지도 부탁드립니다.

ptmono의 이미지

(defun d-test ()
  "씨 확장자인지 검사"
  (let* ((buffername (buffer-name))
	 (c-sourcep (string-match ".*\\.\\(c\\|cc\\|cpp\\|h\\)$" buffername)))
    ; 씨 파일이면 함수를 불러와요
    (when c-sourcep
      (d-test-open-c-source (buffer-file-name)))))
 
 
 
(defun d-test-open-c-source (filename-with-dir)
  "실제 구동함수. FILENAME-WITH-DIR은 파일의 절대경로입니다."
 
  (let* ((source-sidep nil)
	 (header-file-name (concat (file-name-sans-extension filename-with-dir) ".h"))
	 (source-file-name (if (equal (file-name-extension filename-with-dir) "h")
			       (concat (file-name-sans-extension filename-with-dir) ".cpp")
			     (setq source-sidep t)
			     filename-with-dir)))
    ; 파일들을 열어요
    (find-file header-file-name)
    (find-file source-file-name)
 
    ; 화면을 분리해요
    (delete-other-windows)
    (split-window-horizontally)
 
    ; 분리후 커서는 왼쪽에 위치해요
    (switch-to-buffer (file-name-nondirectory header-file-name))
    (other-window 1)
    (switch-to-buffer (file-name-nondirectory source-file-name))
 
    ; 오픈한 소스로 이동해요
    (unless source-sidep
      (other-window 1))))
 
; hook에 추가해요
(push 'd-test find-file-hook)
 
 
------------------------------------------
emacs user

------------------------------------------
emacs user

jinserk의 이미지

잘되네요. 정말 감사합니다.

그런데 한번 파일을 읽고 나서 다른 파일을 C-x C-f 로 열때는 적용이 안되네요.
이것만 해결되면 최고일듯 합니다. ^^

Leo.

jinserk의 이미지

윈도우에는 emacs 23.1 이 깔려 있고, 맥에는 emacs 22.1 이 디폴트로 깔려 있는데, 윈도우 emacs 에서는 파일을 C-x C-f 로 열때마다 적용이 되는데 맥에서는 적용이 안되네요.
emacs 버전이 달라서 그런걸까요?

Leo.

jinserk의 이미지

버그라면 버그일 수 있는데,

C-x C-f 로 새로 여는 파일은 잘 적용됩니다만,
이미 읽은 파일을 buffer 로 전환하는 경우는 적용이 안되네요.
buffer 가 변할때마다 훅킹하면 될듯 해서 찾아봤지만 잘 안됩니다. ㅠ.ㅠ

도움 부탁드립니다.

Leo.

ptmono의 이미지

버젼 22.1의 문제는 확인해 보지 않았습니다. 죄송합니다.

(defadvice iswitchb-buffer (after iswitch-to-buffer-with-c-c++-mode)
  "iswitchb-buffer가 실행된 후에 d-test를 실행해요."
  (d-test))
 
; advice를 활성화해요.
(ad-activate 'iswitchb-buffer)
 
 
 
(defun d-test ()
  "씨 확장자인지 검사"
  (let* ((buffer-name (buffer-name))
	 (c-sourcep (string-match ".*\\.\\(c\\|cc\\|cpp\\|h\\)$" buffer-name))
	 (buffer-file-name (buffer-file-name)))
    ; 씨 파일이면 함수를 불러와요
    (when (and c-sourcep buffer-file-name)
      (d-test-open-c-source buffer-file-name))))
 
 
(defun d-test-open-c-source (filename-with-dir)
  "실제 구동함수. FILENAME-WITH-DIR은 파일의 절대경로입니다."
 
  (let* ((source-sidep nil)
	 (header-file-name (concat (file-name-sans-extension filename-with-dir) ".h"))
	 (source-file-name (if (equal (file-name-extension filename-with-dir) "h")
			       (concat (file-name-sans-extension filename-with-dir) ".cpp")
			     (setq source-sidep t)
			     filename-with-dir)))
    ; 파일들을 열어요
    (find-file header-file-name)
    (find-file source-file-name)
 
    ; 화면을 분리해요
    (delete-other-windows)
    (split-window-horizontally)
 
    ; 분리후 커서는 왼쪽에 위치해요
    (switch-to-buffer (file-name-nondirectory header-file-name))
    (other-window 1)
    (switch-to-buffer (file-name-nondirectory source-file-name))
 
    ; 오픈한 소스로 이동해요
    (unless source-sidep
      (other-window 1))))
 
 
(push 'd-test find-file-hook)

------------------------------------------
emacs user

------------------------------------------
emacs user

jinserk의 이미지

그간 잊고 있다가 생각나서 이제서야 답변을 보게되었습니다.
ptmono 님 자세한 답변 정말 감사합니다.

제가 default 로 switch-to-buffer 를 쓰고 있어서 수정해주신 코드가 동작하지 않는 문제가 있었습니다.
올려주신 코드 앞부분에

(iswitchb-mode 1)
(setq iswitchb-buffer-ignore '("^\\*"))

를 넣어주니 잘 동작합니다.

감사합니다. 덕분에 emacs 쓰기가 훨 즐거워지네요.

Leo.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.