Programing

Emacs에서 * scratch * 버퍼를 다시 열겠습니까?

lottogame 2020. 5. 27. 07:29
반응형

Emacs에서 * scratch * 버퍼를 다시 열겠습니까?


실수 로 Emacs 에서 스크래치 버퍼를 닫은 경우 스크래치 버퍼를 어떻게 만듭니 까?


GNU Emacs 기본 바인딩 :

C-xb *scratch* RET

또는 더 장황하게

M-x switch-to-buffer *scratch* RET

*scratch*버퍼 시작시 선택된 버퍼이고, 주요 모드 갖는다 리스프 상호 작용 . 참고 : *scratch*버퍼 모드 는 변수에 의해 제어됩니다 initial-major-mode.

일반적으로 원하는만큼 "스크래치"버퍼를 만들고 원하는 이름을 지정할 수 있습니다.

C-xb NAME RET

NAME존재하지 않는 경우 버퍼로 전환 합니다. C-xC-w(또는 M-x write-file RET)를 사용 하여 저장해야 할 파일을 선택할 때까지 새 버퍼는 디스크의 파일과 연결되지 않습니다 .

M-x text-mode RET

현재 버퍼의 주 모드를 텍스트 모드로 변경합니다. 사용 가능한 모든 모드를 찾으려면 (즉, 새 패키지없이) 다음을 입력하여 목록을 얻을 수 있습니다.

M-x apropos-command -mode$ RET


내 .emacs에 다음을 추가합니다.

;; bury *scratch* buffer instead of kill it
(defadvice kill-buffer (around kill-buffer-around-advice activate)
  (let ((buffer-to-kill (ad-get-arg 0)))
    (if (equal buffer-to-kill "*scratch*")
        (bury-buffer)
      ad-do-it)))

스크래치 버퍼 를보고 싶지 않다면 Cx Ck을 누르십시오. 그러나 그것을 죽이지 않고 버퍼 목록의 끝에 배치하십시오. 따라서 다음에 새 것을 만들 필요가 없습니다.


이 EmacsWiki 페이지에는 수많은 팁 있습니다.

첫 번째는 다음과 같습니다.

스크래치 버퍼를 재생성하는 매우 간단한 기능 :

(defun create-scratch-buffer nil
   "create a scratch buffer"
   (interactive)
   (switch-to-buffer (get-buffer-create "*scratch*"))
   (lisp-interaction-mode))             

*scratch*iswitchb 모드가 활성화 된 Cx b RET y RET.

*scratch*그렇지 않으면 Cx b RET 만하면 됩니다.


나는 몇 년 전에 처음으로 emacs를 사용하기 시작했습니다. 나는 지금 어디에 있는지 모른다. 그러나 그것은 항상 내 개인 .el 파일에 집이 있었다. Google 검색에서 팝업됩니다.

;;; Prevent killing the *scratch* buffer -- source forgotten
;;;----------------------------------------------------------------------
;;; Make the *scratch* buffer behave like "The thing your aunt gave you,
;;; which you don't know what is."
(save-excursion
  (set-buffer (get-buffer-create "*scratch*"))
  (make-local-variable 'kill-buffer-query-functions)
  (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer))

(defun kill-scratch-buffer ()
  ;; The next line is just in case someone calls this manually
  (set-buffer (get-buffer-create "*scratch*"))

  ;; Kill the current (*scratch*) buffer
  (remove-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
  (kill-buffer (current-buffer))

  ;; Make a brand new *scratch* buffer
  (set-buffer (get-buffer-create "*scratch*"))
  (lisp-interaction-mode)
  (make-local-variable 'kill-buffer-query-functions)
  (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)

  ;; Since we killed it, don't let caller do that.
  nil)
;;;----------------------------------------------------------------------

나는 dwj의 솔루션을 사용했지만 실제로 스크래치 버퍼의 이름을 바꾸면 (예 : 저장하여) 실패했다는 것을 깨달을 때까지 매우 기뻤습니다 .

그런 다음 이것을 채택하여 나에게 효과적입니다.

  (run-with-idle-timer 1 t
    '(lambda () (get-buffer-create "*scratch*")))

나는이 scratch새로운 스크래치 버퍼를 (내가 몇 가지 갖고 싶은) 열기위한 대화 형 명령으로 :

(defun scratch ()
  "create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
  (interactive)
  (let ((n 0)
        bufname)
    (while (progn
             (setq bufname (concat "*scratch"
                                   (if (= n 0) "" (int-to-string n))
                                   "*"))
             (setq n (1+ n))
             (get-buffer bufname)))
  (switch-to-buffer (get-buffer-create bufname))
  (if (= n 1) initial-major-mode))) ; 1, because n was incremented

채택 : http://everything2.com/index.pl?node_id=1038451


(global-set-key (kbd "C-x M-z")
                '(lambda ()
                   (interactive)
                   (switch-to-buffer "*scratch*")))

이렇게하면 *scratch*버퍼로 빠르게 전환 될뿐만 아니라 버퍼를 다시 *scratch*만들고 lisp-interaction-mode실수로 킬 하면 자동으로 활성화 됩니다. 원하는대로 바인딩을 변경하십시오.


Just to note emacs package unkillable-scratch in MELPA will do this. There is also scratch-persist that will automatically save and restore the buffer between sessions.


Like the docstring says, this function will:

Switch to the scratch buffer. If the buffer doesn't exist create it and write the initial message into it."

This will bring a new scratch buffer up which looks like the initial scratch buffer.

(defun switch-buffer-scratch ()
  "Switch to the scratch buffer. If the buffer doesn't exist,
create it and write the initial message into it."
  (interactive)
  (let* ((scratch-buffer-name "*scratch*")
         (scratch-buffer (get-buffer scratch-buffer-name)))
    (unless scratch-buffer
      (setq scratch-buffer (get-buffer-create scratch-buffer-name))
      (with-current-buffer scratch-buffer
        (lisp-interaction-mode)
        (insert initial-scratch-message)))
    (switch-to-buffer scratch-buffer)))

(global-set-key "\C-cbs" 'switch-buffer-scratch)

This is what I use - I have this bound to a convenient keystroke. It sends you to the *scratch* buffer, regardless of whether or not it already exists, and sets it to be in lisp-interaction-mode

(defun eme-goto-scratch () 
  "this sends you to the scratch buffer"
  (interactive)
  (let ((eme-scratch-buffer (get-buffer-create "*scratch*")))
    (switch-to-buffer eme-scratch-buffer)
    (lisp-interaction-mode)))

I prefer to have my scratch buffer be an actual file that is automatically saved, and reopening it is as simple as opening a file. On startup, I kill the default and find my own like this.

(add-hook 'emacs-startup-hook
  (lambda ()
    (kill-buffer "*scratch*")
    (find-file "/Users/HOME/Desktop/.scratch")))

I have a custom kill-buffer function that does essentially the same thing -- reopens my personal scratch saved file and kills the default scratch if I killed the last visible buffer.

I customized a few of the desktop.el functions to load after (kill-buffer "*scratch*") and (find-file "/Users/HOME/Desktop/.scratch") so that the file last visible on exiting Emacs doesn't get buried by the default scratch or buried by my custom scratch when launching Emacs.

I enjoy using auto-save-buffers-enhanced, which automatically saves any file extension that is not specifically excluded:

https://github.com/kentaro/auto-save-buffers-enhanced/blob/master/auto-save-buffers-enhanced.el

(require 'auto-save-buffers-enhanced)
(auto-save-buffers-enhanced t)
(setq auto-save-buffers-enhanced-save-scratch-buffer-to-file-p 1)
(setq auto-save-buffers-enhanced-exclude-regexps '("\\.txt" "\\.el" "\\.tex"))

I use a slight variation of the function by @paprika when I want to create a no-file visiting buffer:

(defun lawlist-new-buffer ()
  "Create a new buffer -- \*lawlist\*"
(interactive)
  (let* (
    (n 0)
    bufname)
  (catch 'done
    (while t
      (setq bufname (concat "*lawlist"
        (if (= n 0) "" (int-to-string n))
          "*"))
      (setq n (1+ n))
      (if (not (get-buffer bufname))
        (throw 'done nil)) ))
  (switch-to-buffer (get-buffer-create bufname))
  (text-mode) ))

I have combined the solutions posted so far into one function:

(defun --scratch-buffer(&optional reset)
  "Get the *scratch* buffer object.
Make new scratch buffer unless it exists. 
If RESET is non-nil arrange it that it can't be killed."
  (let ((R (get-buffer "*scratch*")))
    (unless R
      (message "Creating new *scratch* buffer")
      (setq R (get-buffer-create "*scratch*") reset t))
        (when reset
          (save-excursion
            (set-buffer R)
            (lisp-interaction-mode)
            (make-local-variable 'kill-buffer-query-functions)
            (add-hook 'kill-buffer-query-functions '(lambda()(bury-buffer) nil)
          )))
    R))

To apply this function in your .emacs use:

(--scratch-buffer t)
(run-with-idle-timer 3 t '--scratch-buffer)

This will make the scratch buffer indestructible in the first place, and if saved it will be recreated. Additionally we can use a shortcut function scratch to bring up the buffer quickly:

(defun scratch()
  "Switch to *scratch*.  With prefix-arg delete its contents."
  (interactive)
  (switch-to-buffer (--scratch-buffer))
  (if current-prefix-arg
      (delete-region (point-min) (point-max))
    (goto-char (point-max))))

In the past it has proven useful to know the original startup-directory from which Emacs was started. This is either the value of desktop-dirname or the default-directory local variable of the scratch-buffer:

(defvar --scratch-directory
  (save-excursion (set-buffer "*scratch*") default-directory)
  "The `default-directory' local variable of the *scratch* buffer.")

(defconst --no-desktop (member "--no-desktop" command-line-args)
  "True when no desktop file is loaded (--no-desktop command-line switch set).")

(defun --startup-directory ()
  "Return directory from which Emacs was started: `desktop-dirname' or the `--scratch-directory'.
Note also `default-minibuffer-frame'."
  (if (and (not --no-desktop) desktop-dirname) 
      desktop-dirname
    --scratch-directory))

So --startup-directory will always return the base directory of your makefile, TODO-file etc. In case there is no desktop (--no-desktop commandline-switch or no desktop-file) the --scratch-directory variable will hold directory Emacs was once started under.


find answer in EmacsWiki: http://www.emacswiki.org/emacs/RecreateScratchBuffer

(defun create-scratch-buffer nil
   "create a scratch buffer"
   (interactive)
   (switch-to-buffer (get-buffer-create "*scratch*"))
   (lisp-interaction-mode))

To add to the accepted answer, if you have ILO mode on (and it is autocompleting after C-x b, thus not letting you write *scratch*), then try:

C-xb C-b*scratch* RET

C-x b C-b *scratch* RET


C-xb and then type *scratch* ↩︎

to create a new buffer which is in lisp interaction mode also.

참고URL : https://stackoverflow.com/questions/234963/re-open-scratch-buffer-in-emacs

반응형