2010-06-03 3 views
7

언어에 대한 이맥스 메이저 모드 (일명 mydsl)를 개발 중입니다. 그러나 xahlee 사이트의 기술을 사용하는 것은 어떤 이유로 든 작동하지 않는 것 같습니다. (아마도 구형 이맥스 방언 일 수도 있습니다.)이맥스 메이저 모드에서 올바르게 강조 표시

내가 쟁점으로 삼는 주요 이슈는 (1) 강조 표시가 작동하지 않고 (2) , regexp-opt 회선 사용이 작동하지 않습니다.

나는 GNU 매뉴얼을 검토하고 cc-mode와 elisp 모드를 살펴 봤는데 ... 필자가 필요로하는 것보다 훨씬 더 복잡하다.

;;;Standard # to newline comment 
;;;Eventually should also have %% to %% multiline block comments 

(defun mydsl-comment-dwim (arg) 
    "comment or uncomment" 
    (interactive "*P") 
    (require 'newcomment) 
    (let 
     ((deactivate-mark nil) 
     (comment-start "#") 
     (comment-end "") 
     comment-dwim arg))) 

(defvar mydsl-events 
    '("reservedword1" 
    "reservedword2")) 

(defvar mydsl-keywords 
    '("other-keyword" "another-keyword")) 

;;Highlight various elements 
(setq mydsl-hilite 
     '(
     ; stuff between " 
     ("\"\\.\\*\\?" . font-lock-string-face) 
     ; : , ; { } => @ $ = are all special elements 
     (":\\|,\\|;\\|{\\|}\\|=>\\|@\\|$\\|=" . font-lock-keyword-face) 
     (,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face) 
     (,(regexp-opt mydsl-events 'words) . font-lock-constant-face) 
)) 


(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode") 

(define-derived-mode mydsl-mode fundamental-mode 
    "MYDSL mode is a major mode for editing MYDSL files" 
    ;Recommended by manual 
    (kill-all-local-variables) 
    (setq mode-name "MYDSL script") 
    (setq font-lock-defaults '((mydsl-hilite))) 
    (if (null mydsl-tab-width) 
     (setq tab-width mydsl-tab-width) 
    (setq tab-width default-tab-width) 
    ) 

    ;Comment definitions 
    (define-key mydsl-mode-map [remap comment-dwim] 'mydsl-comment-dwim) 
    (modify-syntax-entry ?# "< b" mydsl-mode-syntax-table) 
    (modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table) 
    ;;A gnu-correct program will have some sort of hook call here. 
) 

(provide 'mydsl-mode) 
+3

: 여기 mydsl-mode에 버퍼를 옳은 일을 할 것으로 보인다 내 편집 한 버전이다? – jrockway

+0

@jrockway - 그의 이맥스 물건은 대체로 견고한 것 같습니다. 무슨 뜻인지 모르겠다. –

+0

나는 jrockway에 대해 말할 수 없다. Emacs에 관한 Xah의 정보를 다작하고 선의로 구할 수 있지만, Emacs의 lisp 규범과 규칙에서 벗어나는 경우가있다. 예 : 'mydsl-comment-dwim'은'comment'의 동작을 커스터마이징하는 잘못된 방법입니다. 비슷하게, Xah의 페이지에서 나는 (함수 적 임에도 불구하고) 인수를 취하고'(interactive "r")'를 사용하여이 지역을 통과하는 'wrap-markup'정의를 신속하게 발견했다. –

답변

9

코드에는 몇 가지 구문 문제가 있지만 거의 정확합니다. 당신은 Xah의 사이트를 잘 풍자는 것을 깨닫는다

; No changes to the simple vars 
(defvar mydsl-events 
    '("reservedword1" 
    "reservedword2")) 

(defvar mydsl-keywords 
    '("other-keyword" "another-keyword")) 

;; I'd probably put in a default that you want, as opposed to nil 
(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode") 

;; Two small edits. 
;; First is to put an extra set of parens() around the list 
;; which is the format that font-lock-defaults wants 
;; Second, you used ' (quote) at the outermost level where you wanted ` (backquote) 
;; you were very close 
(defvar mydsl-font-lock-defaults 
    `((
    ;; stuff between " 
    ("\"\\.\\*\\?" . font-lock-string-face) 
    ;; ; : , ; { } => @ $ = are all special elements 
    (":\\|,\\|;\\|{\\|}\\|=>\\|@\\|$\\|=" . font-lock-keyword-face) 
    (,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face) 
    (,(regexp-opt mydsl-events 'words) . font-lock-constant-face) 
    ))) 

(define-derived-mode mydsl-mode fundamental-mode "MYDSL script" 
    "MYDSL mode is a major mode for editing MYDSL files" 

    ;; fundamental-mode kills all local variables, no need to do it again 
    (setq mode-name "MYDSL script") 

    ;; you again used quote when you had '((mydsl-hilite)) 
    ;; I just updated the variable to have the proper nesting (as noted above) 
    ;; and use the value directly here 
    (setq font-lock-defaults mydsl-font-lock-defaults) 

    ;; when there's an override, use it 
    ;; otherwise it gets the default value 
    (when mydsl-tab-width 
    (setq tab-width mydsl-tab-width)) 

    ;; for comments 
    ;; overriding these vars gets you what (I think) you want 
    ;; they're made buffer local when you set them 
    (setq comment-start "#") 
    (setq comment-end "") 

    (modify-syntax-entry ?# "< b" mydsl-mode-syntax-table) 
    (modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table) 
    ;;A gnu-correct program will have some sort of hook call here. 
) 

(provide 'mydsl-mode) 
+0

modify-syntax-entry 항목은 http://xahlee.org/emacs/elisp_comment_handling.html에서 제공됩니다. modify-syntax-entry 항목은 - 내 생각 엔? 주석 처리를 관리하는 올바른 방법입니다. 그렇지 않으면 emacs는 주석의 코드를 강조 표시합니다. 어쨌든 주석 코드는 작동하지 않습니다. –

+0

주 - comment-dwim 재 맵핑을 사용하면 mydsl-mode에서 comment-dwim이 올바르게 작동합니다. Xahlee에 따르면 modify-syntax-entry 설명자는 주석 강조 표시를 만들어야합니다. –

+0

@PaulNathan 필자가 가지고있는 것 (vars을 설정하고'comment-dwim'을 네이티브로 사용하는 것, 그리고 Xah가 작성한 것) 사이에는 아무런 차이가 없다. 'comment-dwim'은 코드에'#'문자를 사용하여 지역을 주석 처리합니다. 이맥스 23.1. –