2012-12-19 6 views
12

인 이맥스 자동 마이너 모드 this question이 다소 있는데, 확장자을 기반으로 마이너 모드 (또는 그 목록)를 설정하는 [emacs의] 방법이 있습니까? 예를 들어, 주요 모드가 너무확장자가

(setq auto-mode-alist 
    (cons '("\\.notes$" . text-mode) 
     auto-mode-alist)) 

처럼 조작과 제가 이상적으로 할 수 있도록하고 싶습니다 것은 링크의

(setq auto-minor-mode-alist 
    (cons '("\\.notes$" . auto-fill-mode) 
     auto-minor-mode-alist)) 

수락 대답 할 수 있다는 것을 알아 꽤 쉽게 질문에 후크가 포함되어 있습니다 (구체적으로 temp-buffer-setup-hook). 이것을 사용하려면 같은 후크에 기능을 추가 할 필요가 있으므로

(add-hook 'temp-buffer-setup-hook 'my-func-to-set-minor-mode) 

내 질문은 두 가지 :

  1. 이 작업을 수행하는 쉬운 방법이 있나요, 주요 모드와 유사?
  2. 그렇지 않은 경우 어떻게 훅에 대한 함수를 작성합니까?
    1. 정규식과 파일 경로를 비교해야합니다.
    2. 일치하는 경우 원하는 모드를 활성화하십시오 (예 : auto-fill-mode). 솔루션에서

희미한 및 버그 시도 :

(defvar auto-minor-mode-alist() 
    "Alist of filename patterns vs correpsonding minor mode functions, see `auto-mode-alist' 
All elements of this alist are checked, meaning you can enable multiple minor modes for the same regexp.") 
(defun enable-minor-mode-based-on-extension() 
    "check file name against auto-minor-mode-alist to enable minor modes 
the checking happens for all pairs in auto-minor-mode-alist" 
    (when buffer-file-name 
    (let ((name buffer-file-name) 
      (remote-id (file-remote-p buffer-file-name)) 
      (alist auto-minor-mode-alist)) 
     ;; Remove backup-suffixes from file name. 
     (setq name (file-name-sans-versions name)) 
     ;; Remove remote file name identification. 
     (when (and (stringp remote-id) 
       (string-match-p (regexp-quote remote-id) name)) 
     (setq name (substring name (match-end 0)))) 
     (while (and alist (caar alist) (cdar alist)) 
     (if (string-match (caar alist) name) 
      (funcall (cdar alist) 1)) 
     (setq alist (cdr alist)))))) 

(add-hook 'find-file-hook 'enable-minor-mode-based-on-extension) 

참고 :

; Enables the given minor mode for the current buffer it it matches regex 
; my-pair is a cons cell (regular-expression . minor-mode) 
(defun enable-minor-mode (my-pair) 
    (if (buffer-file-name) ; If we are visiting a file, 
     ; and the filename matches our regular expression, 
     (if (string-match (car my-pair) buffer-file-name) 
     (funcall (cdr my-pair))))) ; enable the minor mode 

; used as 
(add-hook 'temp-buffer-setup-hook 
      (lambda() (enable-minor-mode ("\\.notes$" . auto-fill-mode)))) 
+1

이 확장은 보통'으로 작성됩니다. "\\ 메모를 \\'"' . 최종 * 싱글 * 견적을 참조하십시오. –

+0

나는 지옥에 빠질 것이다. 내가 그 타입을 할 때 그것을 놓쳤다. 아마도 후행 백 슬래시 문제를 정확히 설명 할 것입니다. -_- –

답변

11

이 코드는 당신이 원하는 것을 줄 것으로 보인다 비교가 이루어집니다 string-match-p은 비교 중에 case-fold-search 설정을 따릅니다.

+0

오류가 발생해도 대부분의 작업을 이해합니다.> 유효하지 않은 정규 표현식 : "백 슬래시"< "\\. notes $ \\"로 끝나는 나는 유효해야한다고 생각합니다. 그러나, 나는 나의 시도와 동일한 결과를 얻고 있었다. 나는 원래의 질문을 곧 포함시킬 것이다. –

+0

어떻게해야할지 모르지만 이유는 모르겠지만 지금은 모두 작동합니다. 감사!! –

+0

'files.el : set-auto-mode'에서'auto-mode-alist'는'(assoc-default-name auto-mode-alist '문자열 일치)'로 검색됩니다. 이것은 while 루프를 단순화하고 대체 할 수 있습니다. –

2

Trey Jackson의 대답은 매우 견고하고 확장 가능한 해결책 인 것처럼 보이지만 좀 더 간단한 것을 찾고있었습니다. .hmmm 파일을 편집 할 때 다음 코드는 가상의 hmmm-mode을 가능하게 할 것이다 : 당신이 채널의 V 자동 모드 alist` '을 볼 때

(add-hook 'find-file-hook 
      (lambda() 
      (when (string= (file-name-extension buffer-file-name) "hmmm") 
       (hmmm-mode 1))))