2010-03-01 17 views
9

Mi piacerebbe che Emacs mi chieda se voglio salvare un buffer modificato, quando quel buffer non è associato a un file. Per aprire un nuovo buffer (non visitare un file) Ho la seguente funzione nel mio file .emacs:Emacs - Impossibile ottenere buffer-offerta-salvataggio funzionante

;; Creates a new empty buffer 
(defun new-empty-buffer() 
    "Opens a new empty buffer." 
    (interactive) 
    (let ((buf (generate-new-buffer "untitled"))) 
    (switch-to-buffer buf) 
    (funcall (and default-major-mode)) 
    (setq buffer-offer-save t))) 

ho pensato impostazione "buffer-offerta-save" per qualcosa che non avrebbe reso nullo il trucco. Ma ogni volta che uccido il buffer con "kill-this-buffer", viene istantaneamente ucciso senza chiedere nulla.

Questo accade su GNU Emacs 23.1.1

Tutte le idee?

Grazie, W

+0

associare il buffer a un file, in 'new-empty-buffer' risolvere il problema? – Cheeso

risposta

2

cura di aggiungere utilizzo di buffers-offer-save. Nota: la variabile buffer-offer-save is only used upon exiting Emacs.

Si può iniziare con questo codice e personalizzarlo per ciò che si vuole:

(add-to-list 'kill-buffer-query-functions 'ask-me-first) 
(defun ask-me-first() 
    "prompt when killing a buffer" 
    (if (or buffer-offer-save 
      (eq this-command 'kill-this-buffer) 
      (and (buffer-modified-p) (not (buffer-file-name)))) 
     (y-or-n-p (format "Do you want to kill %s without saving? " (buffer-name))) 
    t)) 

Su ulteriore riflessione, che è un po 'pesante perché si ottiene chiesto per tutti buffer che vengono uccisi, e ci sono spesso molti buffer temporanei utilizzati da Emacs. Se si desidera solo essere richiesto quando si tenta di uccidere in modo interattivo un buffer (che non è associato a un file).

È possibile utilizzare questo consiglio, che richiede solo quando si sta cercando di uccidere in modo interattivo un buffer:

(defadvice kill-buffer (around kill-buffer-ask-first activate) 
    "if called interactively, prompt before killing" 
    (if (and (or buffer-offer-save (interactive-p)) 
      (buffer-modified-p) 
      (not (buffer-file-name))) 
     (let ((answ (completing-read 
        (format "Buffer '%s' modified and not associated with a file, what do you want to do? (k)ill (s)ave (a)bort? " (buffer-name)) 
        '("k" "s" "a") 
        nil 
        t))) 
     (when (cond ((string-match answ "k") 
        ;; kill 
        t) 
        ((string-match answ "s") 
        ;; write then kill 
        (call-interactively 'write-file) 
        t) 
        (nil)) 
      ad-do-it) 

     t) 
    ;; not prompting, just do it 
    ad-do-it)) 
+0

assocerebbe il buffer ad un file, in 'new-empty-buffer' risolve il problema? – Cheeso

+0

@Cheeso Bene, farlo in 'new-empty-buffer' metterebbe la richiesta alla fine sbagliata, come in quando crea un nuovo buffer. Detto questo, è possibile modificare 'new-empty-buffer' per impostare un flag che il consiglio/funzione potrebbe disattivare. Credo che mi piacesse una soluzione senza ricorrere all'utilizzo di 'new-empty-buffer'. –

+0

getcha, grazie. – Cheeso

1

Modifica 'new-empty-buffer sembra farlo funzionare come ho inteso con defadvice di Trey.

 
;; Creates a new empty buffer 
(defun new-empty-buffer() 
"Opens a new empty buffer." 
(interactive) 
(let ((buf (generate-new-buffer "untitled"))) 
    (switch-to-buffer buf) 
    (funcall (and default-major-mode)) 
    (put 'buffer-offer-save 'permanent-local t) 
    (setq buffer-offer-save t))) 

Questo rende buffer-offer-save locale permanente nel nostro nuovo buffer, quindi non otterrà uccisi con il resto delle variabili locali quando si cambia modalità principali.

1

buffer-offer-save chiedere di uscire da Emacs ma non chiudere un buffer manualmente non ha senso, quindi perché non "allargare" le sue responsabilità?

(defadvice kill-buffer (around kill-buffer-ask activate) 
    "If `buffer-offer-save' is non-nil and a buffer is modified, 
prompt before closing." 
    (if (and buffer-offer-save (buffer-modified-p)) 
     (when (yes-or-no-p "The document isn't saved. Quit? ") 
     ad-do-it) 
    ad-do-it)) 

Non verrà richiesto se il buffer untitled è stato appena creato. Non verrà richiesto se si utilizza kill-buffer da Elisp. Non richiederà i buffer di sistema Emacs come *Messages*. Ma verrà richiesto se hai creato un buffer vuoto e scritto qualcosa in esso.

Vedere anche my answer on creating an empty buffer.

Problemi correlati