2015-12-23 4 views

ответ

5

Вы можете упростить ваш код:

(defun my-file-contents (filename) 
    "Return the contents of FILENAME." 
    (with-temp-buffer 
    (insert-file-contents filename) 
    (buffer-string))) 

редактирования: Хотя в данном конкретном случае, я вижу, что json-read-file уже определен , который вырезает среднего человека.

Emacs 24.5 определяет его следующим образом:

(defun json-read-file (file) 
    "Read the first JSON object contained in FILE and return it." 
    (with-temp-buffer 
    (insert-file-contents file) 
    (goto-char (point-min)) 
    (json-read))) 
0

Я закончил с этим кодом:

(defun read-file (filename) 
    (save-excursion 
    (let ((new (get-buffer-create filename)) (current (current-buffer))) 
     (switch-to-buffer new) 
     (insert-file-contents filename) 
     (mark-whole-buffer) 
     (let ((contents (buffer-substring (mark) (point)))) 
     (kill-buffer new) 
     (switch-to-buffer current) 
     contents)))) 
0

Для JSON просто использовать json-read-file. Для общих файлов f библиотека предоставляет f-read-bytes и f-read-text:

(f-read-text "file.txt" 'utf-8) 
0

Для тех, кто вам не нравится писать совсем так много кода, вы можете использовать f-read из f библиотеки (не входит в стандартную библиотеку). Это исходит из этого вопроса: elisp: read file into list of lists

Смежные вопросы