파일과 동일한 주 모드를 가진 * scratch * 버퍼 사이를 빠르게 전환하려면 어떻게해야합니까?


24

코드 작업 중에 때때로 * scratch * 버퍼를 빠르게 열어 해당 코드 파일에서 코드 조각을 붙여 넣는 것이 유용합니다.

Perl 스크립트를 작업 중이라면 in으로 * scratch * 버퍼를 빠르게 열고 싶습니다 cperl-mode. 원래 작업했던 코드 버퍼로 빠르게 되돌아가는 것도 좋습니다.

답변:


26

아래 기능을 선택한 키 바인딩에 바인딩하는 것이 편리합니다. 현재 FILE버퍼에서 작업중인 경우 아래 함수를 호출하면 호출되는 FILE주요 모드 별 * scratch * 버퍼 *scratch-MAJOR-MODE*와 버퍼 간에 전환됩니다 FILE.

문제의 예가 주어지면이라는 Perl 스크립트 myperl.pl를 사용하는 경우이 함수를 호출하면 myperl.pl과 사이를 전환 *scratch-cperl-mode*합니다.

(defun modi/switch-to-scratch-and-back (&optional arg)
  "Toggle between *scratch-MODE* buffer and the current buffer.
If a scratch buffer does not exist, create it with the major mode set to that
of the buffer from where this function is called.

        COMMAND -> Open/switch to a scratch buffer in the current buffer's major mode
    C-0 COMMAND -> Open/switch to a scratch buffer in `fundamental-mode'
    C-u COMMAND -> Open/switch to a scratch buffer in `org-mode'
C-u C-u COMMAND -> Open/switch to a scratch buffer in `emacs-elisp-mode'

Even if the current major mode is a read-only mode (derived from `special-mode'
or `dired-mode'), we would want to be able to write in the scratch buffer. So
the scratch major mode is set to `org-mode' for such cases.

Return the scratch buffer opened."
  (interactive "p")
  (if (and (or (null arg)               ; no prefix
               (= arg 1))
           (string-match-p "\\*scratch" (buffer-name)))
      (switch-to-buffer (other-buffer))
    (let* ((mode-str (cl-case arg
                       (0  "fundamental-mode") ; C-0
                       (4  "org-mode") ; C-u
                       (16 "emacs-lisp-mode") ; C-u C-u
                       ;; If the major mode turns out to be a `special-mode'
                       ;; derived mode, a read-only mode like `help-mode', open
                       ;; an `org-mode' scratch buffer instead.
                       (t (if (or (derived-mode-p 'special-mode) ; no prefix
                                  (derived-mode-p 'dired-mode))
                              "org-mode"
                            (format "%s" major-mode)))))
           (buf (get-buffer-create (concat "*scratch-" mode-str "*"))))
      (switch-to-buffer buf)
      (funcall (intern mode-str))   ; http://stackoverflow.com/a/7539787/1219634
      buf)))

이 기능에 대해 금지 모드, 용어 모드 및 기타 편집 불가능 모드를 제외하려면 어떻게해야합니까?
— godblessfq

@ godblessfq 나는 최근에 내 구성 에서 이것을 고쳤습니다 . 지금 당신은 거기에서 그 버전을 얻을 수 있습니다. 컴퓨터에 도착하면이 답변을 업데이트하겠습니다.
— Kaushal Modi

@ godblessfq 답변을 업데이트했습니다. 그것을 시도하고 그것이 당신을 위해 작동하는지 확인하십시오.
— Kaushal Modi

매력처럼 작동합니다!
— godblessfq
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.