레이블이 이맥스인 게시물을 표시합니다. 모든 게시물 표시
레이블이 이맥스인 게시물을 표시합니다. 모든 게시물 표시

2008년 10월 21일

이맥스 프레임 빨리 바꾸기

빠른 답변: other-frame 함수가 C-x 5 o로 바인딩되어 있습니다만, 그것에 대하여 얘기하고자 하는 것은 아닙니다.

저는 코드 작성할 때 키보드에서 다른 곳으로 집중을 빼앗기는 것을 싫어합니다. 전, 키보드와 마우스를 넘나들면서도 남들보다 빨리 코딩할 자신이 없습니다.

그래서 될 수 있으면 마우스를 안 쓰려고 하는데, 그건 쥐들이 싫어서 그런 것이 아니라 키보드로 코드 쓰는 것이 제일 편하기 때문입니다. 키보드 없이 코딩하는 것을 좋아하시는 분이 있을지도 모르겠습니다. (그치만, 음성 인식 시스템으로 펄 코딩하는 비디오 보셨나요?)

어쨌든 저는 그놈 작업 공간 하나에 이맥스 프레임을 6개 올려 놓고 사용합니다. 이맥스 프레임이라는 것이 X 상에서는 창이지요. 그리고 알트 탭 신공으로는 창 바꾸기가 너무 힘듭니다. 전 알트 탭 신공에 능하지 않아서 알트를 누르고 있으면서 탭을 몇 번 눌러야 제가 원하는 창으로 갈 수 있는지를 모릅니다. 어떤 분은 한번에 10개 이상 창을 띄워 놓고 작업하시더라구요.

웹에 찾아보니까 Joseph Perla라는 분의 방법을 이용할 수 있겠더라구요. 아주 재미있는 방법인데, 제가 창을 옮길 때마다 코드를 수정하고 싶지는 않더라구요.

특정 방향에 있는 프레임으로 옮기면 정말 좋을 것 같다고 생각했죠. X 윈도 시스템을 좀 더 파 볼까 생각했지만, 어차피 화면에 놓여 있는 창들은 모두 이맥스 프레임인데, ELisp으로 짜는 것이 더 낫고, 더 재미있겠다고 생각했습니다.

그걸 하기 위한 이맥스 코드를 작성했습니다. 슈퍼키+왼쪽 화살표로 왼쪽에 있는 가장 가까운 이맥스 프레임(창)으로 이동합니다. 다른 화살표도 마찬가지.

ELisp 코드

아래 코드를 복사해서 붙인 다음 ELisp 코드를 두는 곳에 저장하세요.

;; directed-switch-to-frame.el -- Switching to frames with direction.

;; Keywords: frame, direction

;; Move 4 direction to switch emacs frames.

(defun cartesian-to-polar (cartesian)
  "Convert cartesian coordinate to polar coordinate."
  (let ((x (float (car cartesian))) (y (float (cdr cartesian))))
    (cons
     (sqrt (+ (* x x) (* y y)))
     (cond ((and (= x 0) (> y 0)) (* pi 0.5))
           ((and (= x 0) (< y 0)) (* pi 1.5))
           ((< x 0) (+ (atan (/ y x)) pi))
           ((and (> x 0) (< y 0)) (+ (atan (/ y x)) (* pi 2)))
           ((> x 0) (atan (/ y x)))
           (t nil)))))

(defun switch-to-frame-with-direction (dir-in-deg var-in-deg next)
  "Switch to frame with direction with variation."
  (let ((f (sort (delq nil
                       (let ((list-of-frames (mapcar (lambda (each-frame)
                                                       (cons each-frame
                                                             (cons (+ (frame-parameter each-frame 'left)
                                                                      (/ (frame-pixel-width each-frame) 2))
                                                                   (+ (frame-parameter each-frame 'top)
                                                                      (/ (frame-pixel-height each-frame) 2)))))
                                                     (cons (selected-frame) 
                                                           (delete-if (lambda (item)
                                                                        (or (eq (selected-frame) item)
                                                                            (eq nil (frame-parameter item 'window-id))))
                                                                        (visible-frame-list)))))
                             (dir-in-rad (degrees-to-radians (mod dir-in-deg 360)))
                             (var-in-rad (degrees-to-radians (mod var-in-deg 360))))
                         (mapcar
                          (lambda (each-frame)
                            (let ((polar (cartesian-to-polar (cons (- (cadr each-frame) (cadr (car list-of-frames)))
                                                                   (- (cddr each-frame) (cddr (car list-of-frames)))))))
                              (when (or (not (numberp (cdr polar)))
                                        (< (- (* 2 pi) (abs (- (cdr polar) dir-in-rad))) var-in-rad)
                                        (< (abs (- (cdr polar) dir-in-rad)) var-in-rad))
                                (cons (car each-frame) polar))))
                          (cdr list-of-frames))))
                 (lambda (a b)
                   (or (not (numberp (cddr b)))
                       (and (numberp (cddr a))
                            (< (cadr a) (cadr b))))))))
    (cond ((not f) nil)
          ((cddar f) (x-focus-frame (caar f)))
          (t (while (not (memq (selected-frame) (mapcar 'car f)))
               (other-frame next))))))

(defun switch-to-right-frame ()
  (interactive)
  (switch-to-frame-with-direction 0 60 -1))

(defun switch-to-down-frame ()
  (interactive)
  (switch-to-frame-with-direction 90 60 -1))

(defun switch-to-left-frame ()
  (interactive)
  (switch-to-frame-with-direction 180 60 1))

(defun switch-to-up-frame ()
  (interactive)
  (switch-to-frame-with-direction 270 60 1))

(provide 'directed-switch-to-frame)

죄송합니다만, 아직 ELisp 코드를 잘 짜는 것은 아닙니다.

키 바인딩

다음 코드를 이맥스 설정 파일인 .emacs에 넣습니다.

;; Moving between frames
(when (load "directed-switch-to-frame")
  (global-set-key [s-left] 'switch-to-left-frame)
  (global-set-key [s-right] 'switch-to-right-frame)
  (global-set-key [s-up] 'switch-to-up-frame)
  (global-set-key [s-down] 'switch-to-down-frame))

되는지 해 볼까요?

이맥스를 새로 시작하고 C-x 5 2를 눌러서 프레임을 여러 개 만듭니다. 보통 슈퍼키는 윈도키로 바인딩 되어 있으므로 윈도키를 누른 상태에서 방향키를 눌러보세요. 잘 되나요?

이맥스 창으로만 움직일 수 있습니다. 그리고 다른 이맥스 인스턴스의 프레임으로는 이동할 수 없습니다.

만약에 프레임의 중심 좌표가 정확하게 겹친 상태에서 프레임이 없는 방향으로 이동하라고 한다면 겹쳐진 창을 순환시킬 수 있습니다. 전체 화면 프레임들을 여러 개 겹쳐 놓고 작업하는 상황에서 유용하게 쓸 수 있습니다. 알트 탭을 누르는 것보다 더 빨리 움직일 수 있습니다. 반대쪽 방향을 누르면 반대로 순환합니다.

Switch Emacs Frames Quickly

Quick answer: Function other-frame is bound to C-x 5 o. But I'm not talking about it.

I don't like to lose focus on my keyboard when I write code. I'm not effective enough to work well while switching between keyboard and mouse.

So I try not to use mouse if possible, not because I hate mice but I can write code easier with keyboard only. Someone may prefer to write code without keyboard though. (But has anybody here watched the video about writing perl code with voice recognition system?)

Anyway, I open about 6 emacs frames–which are windows of X– in a single GNOME workspace. And it's very hard to switch between frames with Alt-Tabs. I don't know how many Tabs with Alt key pressed I need for switching to my desired frame. I saw someone open more than 10 frames at a time.

I searched and found Joseph Perla's solution. His solution was very interesting, but I'd rather not change source code whenever I move window positions.

I thought it would be great if I could switch to the frame on some direction. I could research X Window system more, but I chose ELisp for that, because every window on the screen was Emacs frame and I enjoyed learning ELisp.

Well, this is ELisp code to make it possible. I bound super-left key to switch to the nearest frame on the left side of the current frame, and so on.

ELisp Code

You can copy and paste the following code and save it to your ELisp place.

;; directed-switch-to-frame.el -- Switching to frames with direction.

;; Keywords: frame, direction

;; Move 4 direction to switch emacs frames.

(defun cartesian-to-polar (cartesian)
  "Convert cartesian coordinate to polar coordinate."
  (let ((x (float (car cartesian))) (y (float (cdr cartesian))))
    (cons
     (sqrt (+ (* x x) (* y y)))
     (cond ((and (= x 0) (> y 0)) (* pi 0.5))
           ((and (= x 0) (< y 0)) (* pi 1.5))
           ((< x 0) (+ (atan (/ y x)) pi))
           ((and (> x 0) (< y 0)) (+ (atan (/ y x)) (* pi 2)))
           ((> x 0) (atan (/ y x)))
           (t nil)))))

(defun switch-to-frame-with-direction (dir-in-deg var-in-deg next)
  "Switch to frame with direction with variation."
  (let ((f (sort (delq nil
                       (let ((list-of-frames (mapcar (lambda (each-frame)
                                                       (cons each-frame
                                                             (cons (+ (frame-parameter each-frame 'left)
                                                                      (/ (frame-pixel-width each-frame) 2))
                                                                   (+ (frame-parameter each-frame 'top)
                                                                      (/ (frame-pixel-height each-frame) 2)))))
                                                     (cons (selected-frame) 
                                                           (delete-if (lambda (item)
                                                                        (or (eq (selected-frame) item)
                                                                            (eq nil (frame-parameter item 'window-id))))
                                                                        (visible-frame-list)))))
                             (dir-in-rad (degrees-to-radians (mod dir-in-deg 360)))
                             (var-in-rad (degrees-to-radians (mod var-in-deg 360))))
                         (mapcar
                          (lambda (each-frame)
                            (let ((polar (cartesian-to-polar (cons (- (cadr each-frame) (cadr (car list-of-frames)))
                                                                   (- (cddr each-frame) (cddr (car list-of-frames)))))))
                              (when (or (not (numberp (cdr polar)))
                                        (< (- (* 2 pi) (abs (- (cdr polar) dir-in-rad))) var-in-rad)
                                        (< (abs (- (cdr polar) dir-in-rad)) var-in-rad))
                                (cons (car each-frame) polar))))
                          (cdr list-of-frames))))
                 (lambda (a b)
                   (or (not (numberp (cddr b)))
                       (and (numberp (cddr a))
                            (< (cadr a) (cadr b))))))))
    (cond ((not f) nil)
          ((cddar f) (x-focus-frame (caar f)))
          (t (while (not (memq (selected-frame) (mapcar 'car f)))
               (other-frame next))))))

(defun switch-to-right-frame ()
  (interactive)
  (switch-to-frame-with-direction 0 60 -1))

(defun switch-to-down-frame ()
  (interactive)
  (switch-to-frame-with-direction 90 60 -1))

(defun switch-to-left-frame ()
  (interactive)
  (switch-to-frame-with-direction 180 60 1))

(defun switch-to-up-frame ()
  (interactive)
  (switch-to-frame-with-direction 270 60 1))

(provide 'directed-switch-to-frame)

Sorry, I'm not good at writing ELisp code yet.

Key binding

I put the following code to my .emacs file.

;; Moving between frames
(when (load "directed-switch-to-frame")
  (global-set-key [s-left] 'switch-to-left-frame)
  (global-set-key [s-right] 'switch-to-right-frame)
  (global-set-key [s-up] 'switch-to-up-frame)
  (global-set-key [s-down] 'switch-to-down-frame))

Enjoy

Restart your emacs and open multiple frames with C-x 5 2. Usually the super key is bound to the Windows key. So try Win-Left, Win-Up, Win-Right, Win-Down to switch frames. Does it work?

It doesn't move to windows other than Emacs frames. And it doesn't move to Emacs frames of other emacs instances.

If center coords of frames are same, the focus cycles if there are no other frames on that direction. This is useful if you opened multiple full screen frames. You can switch more quickly than Alt-Tab. Opposite direction will cycle the opposite way. Isn't that cool? :)

2008년 5월 14일

이맥스 사전 패키지 dictionary

!! 저는 이 사전 패키지보다 구글 사전과 이맥스를 붙인 것을 더 자주 사용합니다. 제 블로그에서 찾아주세요. !!

소개

이맥스에서 메일을 읽거나 웹을 돌아다니다 보면, 사전을 찾아야 할 때가 종종 있다. 그럴 때마다 종이 사전을 뒤적거리거나, 전자 사전을 뒤적거리거나, 혹은 다른 사전 응용 프로그램이나 웹 사전을 이용해 본 경험이 있을 것이다. 이맥스를 사용하는 도중에 가급적이면 이맥스 밖으로 눈을 돌리는 행동을 삼가야 한다. 따라서 이맥스에서 나가지 않고, 사전을 뒤적거릴 수 있는 사전 패키지인 dictionary를 소개하고자 한다.

설치

dictionary 패키지는 RFC 2229에 따라 동작하는 사전 서버와 통신이 필요하므로 사전 서버와의 네트워크 연결이 필요하다. 기본으로 지정된 사전 서버도 잘 동작하므로, 인터넷에 연결되어 있는 PC면 충분하다.

필자는 젠투 리눅스를 이용하므로 다음과 같이 설치를 수행하였다.

# emerge app-emacs/dictionary

데비안을 이용한다면 다음과 같이 패키지 설치를 할 수 있다.

# apt-get install dictionary-el

직접 설치하려면 홈페이지에서 다운로드 한 뒤 적당한 디렉토리에 넣고, 필요하다면 컴파일을 하면 된다. 다른 패키지들의 설치 방법과 동일하다.

사용

다음과 같은 키바인딩을 이용하면 편리하다. 젠투의 경우에는 패키지를 설치하면 기본적으로 다음과 같은 키바인딩이 된다.

;; key bindings
(global-set-key "\C-cs" 'dictionary-search)
(global-set-key "\C-cm" 'dictionary-match-words)

이제 C-c s를 누르고 찾고자 하는 단어를 찾으면 사전 검색 결과가 화면에 나타난다.

커스터마이징

M-x customize-group RET dictionary RET을 하면 커스터마이징 페이지를 볼 수 있다. 여기서 사전 서버를 변경한다거나 다른 세팅들을 할 수 있다.

2007년 7월 15일

이맥스에서 지메일의 SMTP 이용하기

나는 오페라 웹 브라우저의 메일 클라이언트인 M2이맥스를 메일 클라이언트로 이용한다. 이맥스를 메일 클라이언트로 활용할 때 지메일의 SMTP를 이용하여 메일을 보낼 수 있는데 이 방법을 소개하고자 한다.

지메일의 받고 보내는 서버는 다음과 같다.

서버 종류설정
받는 메일 서버 (POP3)pop.gmail.com
SSL: 사용함
포트: 995
보내는 메일 서버 (SMTP)smtp.gmail.com (인증 사용)
STARTTLS: 사용함
포트: 465와 587

이것은 지메일에서 POP 신청을 한 후에 이용 가능하다. 만약 이미 지메일을 다른 메일 클라이언트에서 이용하고 있다면 이미 신청을 한 것이다. 그렇지 않다면 지메일 설정에 가서 POP 신청을 하면 된다. 신청을 하면 POP3 뿐만 아니라 SMTP까지 이용할 수 있다.

그렇다면 지메일의 SMTP를 이용하는 방법을 알아보자. 시작 스크립트에 등록하는 방법을 이용할 수도 있고, Customize를 이용하는 방법이 있는데, Customize를 이용하는 방법을 알아보자.

  1. M-x customize-group RET mail RET을 한다.
  2. 사용자 이름 등 필요한 설정을 하고 C-x C-s를 하여 설정을 저장한다.
  3. M-x customize-variable RET send-mail-function RET을 한 뒤 smtpmail-send-it으로 설정한다. 이것을 하지 않으면 sendmail 프로그램을 통하여 메일이 전송된다. C-x C-s로 저장한다.
  4. M-x custoimze-variable RET message-send-mail-function RET을 한 뒤 message-smtpmail-send-it을 설정한다. C-x C-s로 저장한다.
  5. ~/.signature 파일에 자신의 메일 서명을 쓰고 저장한다.
  6. M-x customize-group RET smtpmail RET를 한다.
  7. smtpmail-default-smtp-server와 smtpmail-smtp-server를 smtp.gmail.com 으로 설정한다.
  8. smtpmail-smtp-service를 587로 설정한다.
  9. smtpmail-starttls-credentials에서 State를 누르고 :키를 눌러 LISP 형태로 나타나게 한다. 이렇게 하는 이유는 nil을 입력할 수 있는 방법이 없게 되어 있는 문제점이 있기 때문이다.
  10. 빈 칸에 '(("smtp.gmail.com" 587 nil nil))를 입력한다.
  11. smtpmail-auth-credentials에 Value Menu를 누르고 Repeat를 선택한다.
  12. INS 버튼을 누른 다음에 필요한 내용을 집어 넣는다. 여기서 사용자 이름에는 반드시 @gmail.com까지 포함시켜 완전한 이메일 주소를 넣어야 한다.
  13. 패스워드는 넣지 않으면 보낼 때마다 물어본다.
  14. C-x C-s를 눌러 설정 파일을 저장한다.

만일 패스워드를 매번 입력하는 것이 귀찮지만 이맥스 설정 파일에 패스워드를 집어넣는 것이 마음 편하지 않다면 따로 netrc 형태의 파일을 만들고 거기에 집어넣고 permission을 변경하는 방법이 있다.

임의의 파일을 하나 생성하여 퍼미션을 600으로 준 뒤 다음과 같이 입력하고 저장하고 M-x customize-variable RET smtp-auth-credentials RET를 하고 Value Menu를 눌러 파일 형태를 선택하고 그 파일 이름을 입력하면 된다.

machine smtp.gmail.com login username@gmail.com password PASSWORD

이제 C-x m을 눌러 메일을 작성한 뒤 C-c C-c를 눌러서 메일을 전송하면 된다. 먼저 자기 자신에게 테스트 메일을 보내본다. 언어 설정 등이 제대로 되어 있다면 한글로 보낸 경우에도 깨지지 않고 메일이 잘 전송될 것이다.

VM을 이용하는 사람들은 Bill Clementson's Blog: VM and Gmail Setup for Dummies를 참고한다.

2007년 3월 31일

쓸만한 이맥스 rhtml 모드

루비 온 레일스 프로젝트를 할 때 이맥스로 rhtml 파일을 편집해야 할 일이 많다. 그러나 기본 설정의 이맥스는 rhtml을 편집할 경우에는 자동으로 모드를 전환해주지 않는다. rails.el을 사용하는 경우에는 html 모드를 주모드로 하게 되는데 내장된 루비 코드가 인식이 되지 않기 때문에 별로 좋지 않다. 게다가 nxml-mode같이 html을 다루기 더 좋은 모드도 있는데 html 모드를 쓰고 싶지 않을 것이다. 이것에 대하여 꽤 괜찮은 글이 있어서 포스팅 해 본다.

문제는 여러 모드 중에서 하나를 선택하여야 한다는 것이고 하나를 선택하면 다른 것의 특징은 잃어버리게 된다는 것이다. 이 글에서 트랙백하고 있는 페이지의 저자는 Rinari라고 하는 패키지가 꽤 좋은 특징이 있는데 다른 특징이 빈약하다고 한다.

리스프의 강력함 덕분에 해결책은 간단하다. 다음과 같은 방법으로 nxml 모드 + 지능적 루비 코드 다루기를 할 수 있다. 원문에서 조금의 추가/수정을 가하였다.

  1. Rinari 패키지에서 rhtml 부분을 내려받는다.
    svn checkout svn://pstickne@rubyforge.org/var/svn/rinari/trunk/rhtml
    
  2. nxml-mode를 설치하지 않았다면 설치한다. 젠투 리눅스를 사용하는 경우라면 다음 명령을 입력한다.
    emerge nxml-mode
    
  3. rails.el 이 깔려 있지 않다면 설치한다.
  4. ruby-electric이 깔려 있지 않다면 설치한다.
  5. $HOME/.emacs 파일에 다음을 추가한다.
    (add-to-list 'load-path "~/.emacs.d/rhtml/")
    (add-to-list 'load-path "~/.emacs.d/emacs-rails/")
    (require 'nxml-mode)
    (require 'rhtml-mode)
    (require 'rails)
    (add-hook 'nxml-mode-hook
       (lambda ()
         (define-key nxml-mode-map "r" 'newline-and-indent)
         (setq indent-tabs-mode nil)
         (setq local-abbrev-table nxml-mode-abbrev-table)
         (message "My nxml-mode customizations loaded")))
    
  6. rhtml/rhtml-mode.el 파일을 편집하여 (define-derived-mode rhtml-mode html-mode “RHTML” 부분에서 html-modenxml-mode 로 바꾼다.
  7. rails.el 파일을 열어서 (setq auto-mode-alist (cons '("\.rhtml$" . html-mode) ...)...) 부분을 찾아서 html-code를 rhtml-code로 바꾸어 준다.
  8. 이제 rhtml 파일을 열때마다 꽤 괜찮은 화면이 나온다.

이렇게 하니까 삽입된 루비 코드가 제대로 하이라이팅이 되어 나오지 않았다. 따라서 6번 항목을 원래대로 돌리고 사용하고 있는데, 결국 html 대신에 nxml-mode로 사용하는 것은 실패한 셈이다.

여기에는 rhtml을 validation 할 때 발생하는 오류를 제거하기 위한 nxml-mode의 패치가 있다. 단, 이 패치로 모든 문제점이 해결되지는 않는다.