답변:
아래 기능을 선택한 키 바인딩에 바인딩하는 것이 편리합니다. 현재 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)))