2010-05-12 6 views
16

Ich arbeite an einer benutzerdefinierten .emacs-Datei, die ich auf mehreren verschiedenen Computern verwenden können. Ich möchte einen Modus laden können, wenn es auf dem System vorhanden ist. Wenn es nicht existiert, möchte ich, dass Emacs den Fehler nicht mehr anzeigt: File error: Cannot open load file, X.In Lisp, vermeiden "Ladedatei nicht öffnen" bei Verwendung erforderlich

Zum Beispiel:

(require 'darkroom-mode) 

Ergebnisse in:

File error: Cannot open load file, darkroom-mode 

ich file-exists-p bin mit testen, ob bestimmte andere Dateien vorhanden sind, aber für diesen Test würde ich nehme an, ich brauche meine Last-Pfad suchen . Ich bin Lisp neu, also stolpert mich das.

+3

Oder nur '(ignore-errors ('was auch immer')). – jrockway

Antwort

25

Wenn Sie nur require aus der Ausgabe um einen Fehler halten möchten, können Sie tun:

; third arg non-nil means to not signal an error if file not found 
; a symbol provides documentation at the invocation (and is non-nil) 
(require 'darkroom-mode nil 'noerror) 

Aus der Dokumentation (Ch f benötigen RET):

require is a built-in function in `C source code'. 

(require feature &optional filename noerror) 

If feature feature is not loaded, load it from filename. 
If feature is not a member of the list `features', then the feature 
is not loaded; so load the file filename. 
If filename is omitted, the printname of feature is used as the file name, 
and `load' will try to load this name appended with the suffix `.elc' or 
`.el', in that order. The name without appended suffix will not be used. 
If the optional third argument noerror is non-nil, 
then return nil if the file is not found instead of signaling an error. 
Verwandte Themen