2011-11-27 89 views

回答

2

如果你只是想這樣當您打開與CX比照文件的工作,那麼你可能只需要附上您的行爲find-file,但更深層次的下跌,我相信insert-file-contents是什麼最終讀取文件。

粗略的看起來似乎沒有顯示任何適當的鉤子,所以你可以看看在建議之前這樣做。

(defadvice insert-file-contents 
    (before my-before-insert-file-contents-advice) 
    "Process files externally before reading them." 
    (let ((filename (expand-file-name (ad-get-arg 0)))) 
    (message "About to read file %s" filename) 
    ;; your code here. 

    ;; ;; stupid unsafe example: 
    ;; (let ((file (shell-quote-argument filename)) 
    ;;  (tempfile (shell-quote-argument (make-temp-file "some-prefix-")))) 
    ;; (shell-command (format "sort %s >%s" file tempfile)) 
    ;; (shell-command (format "mv %s %s" tempfile file))) 
    )) 
(ad-activate 'insert-file-contents) 

您可能想詳細說明您的要求,以防您實際上不需要打開原始文件? (我認爲這是一個可怕的想法,坦率地說,我當然不會使用這樣的代碼!)

例如,您可以讀取原始文件,在緩衝區內處理它(也許使用shell-command-on帶替換標誌的區域),並將緩衝區設置爲未修改。這樣,如果您對該文件進行其他編輯,則僅可能保存shell命令所做的更改,而僅將文件加載到編輯器中的操作實際上並未對其進行修改。

無論如何,我相信你會在你的代碼中實現合理的備份過程,並且在測試時會有很多偏執的行爲!

3

我曾經工作過的地方有一些二進制文件,我想在emacs中查看。我這樣做的方式是添加到jka-compr-compression-info-list像編輯的AppleScript如下:

(add-to-list 'jka-compr-compression-info-list 
      ["\\.scpt\\'" 
       "Compiling" "osacompile-helper.sh" nil 
       "Decompiling" "osacompile-helper.sh" ("-d") 
       nil nil "Fasd"]) 
(jka-compr-update) 

這裏osacompile-helper.sh只是圍繞osacompile一個小殼包裝和osadecompilestdin讀取和寫入stdout(這是必需的)。您還需要打開auto-compression-mode,但我認爲這是默認設置。如果使用自定義界面更改jka-compr-compression-info-list,而不是直接設置它,則不必調用jka-compr-update

+1

這是正確的做法,它就像打開'.gz'文件等工作的方式。 –

1

您可以用shell-command調用外部程序,並將輸出指向新的緩衝區。一個最小的工作的例子是:

(defun my-find-and-process-file() 
    (interactive) 
    (let* ((file (read-file-name "File name: ")) 
     (buf (pop-to-buffer file))) 
    (shell-command (format "cat %s" file) buf))) 

替換cat與你的程序的名稱。這將創建一個緩衝區並將其填充到程序的輸出中。如果一個名爲文件名的緩衝區已經存在,它將覆蓋它。如果這是一種可能性,您將需要通過添加後綴或其他內容來將緩衝區名稱更改爲安全。此代碼也不會觸發任何find-file掛鉤,因此您必須手動選擇模式,或修改代碼以便爲您執行此操作。

相關問題