Come posso ricevere un avviso prima di uccidere un buffer temporaneo in Emacs?

StackOverflow https://stackoverflow.com/questions/86963

  •  01-07-2019
  •  | 
  •  

Domanda

Più di una volta ho perso il lavoro uccidendo accidentalmente un buffer temporaneo in Emacs.Posso impostare Emacs per ricevere un avviso quando interrompo un buffer non associato a un file?

È stato utile?

Soluzione

Crea una funzione che ti chieda se sei sicuro quando il buffer è stato modificato e non è associato a un file.Quindi aggiungi quella funzione all'elenco kill-buffer-query-functions.

Guardando la documentazione per Nome file buffer capisci:

  • un buffer non visita un file se e solo se la variabile buffer-file-name è zero

Usa questa intuizione per scrivere la funzione:

(defun maybe-kill-buffer ()
  (if (and (not buffer-file-name)
           (buffer-modified-p))
      ;; buffer is not visiting a file
      (y-or-n-p "This buffer is not visiting a file but has been edited.  Kill it anyway? ")
    t))

E poi aggiungi la funzione all'hook in questo modo:

(add-to-list 'kill-buffer-query-functions 'maybe-kill-buffer)

Altri suggerimenti

(defun maybe-kill-buffer ()
  (if (and (not buffer-file-name)
           (buffer-modified-p))
      ;; buffer is not visiting a file
      (y-or-n-p (format "Buffer %s has been edited.  Kill it anyway? "
                        (buffer-name)))
    t))

(add-to-list 'kill-buffer-query-functions 'maybe-kill-buffer)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top