読者です 読者をやめる 読者になる 読者になる

auto-complete.el にやてふ拡張を作ってみた

テキスト入力中に補完候補を自動的に表示してくれる auto-complete.el をリリースしました — ありえるえりあ

auto-complete.el に感動しすぎて泣いてしまったので,やてふ拡張を作ってみました.

とは言っても,オリジナルのやてふのリストをつなげただけなんですが….

TeX でマクロを作成するときに,めちゃくちゃ捗りすぎて,うれしくなっちゃいました.

まだ試作段階でまともに使っていないので,足りないものはどんどん追加していく予定です.

;;; auto-complete-yatex.el --- A YaTeX extention for Auto completion

;; Copyright (C) 2009  Munehiro Yamamoto

;; Author: Munehiro Yamamoto <munepixyz あっとまーく じーめーるどっとこむ>
;; Keywords: YaTeX LaTeX TeX 
;; Version: 0.2.0.2

;;; History:

;; 2008-03-04 Munehiro Yamamoto <munepixyz あっとまーく じーめーるどっとこむ>
;;
;;      * added original YaTeX keywords to ac-yatex-keywords
;;      * defvar ac-yatex-user-keywords
;;
;; 2008-03-01 Munehiro Yamamoto <munepixyz あっとまーく じーめーるどっとこむ>
;;
;;      * first release
;;

(require 'auto-complete)
(require 'yatex)
(require 'yatexadd)
;(require 'yatexmth)
;(require 'yatexpkg)
;(require 'yatexsec)

(defvar ac-yatex-user-keywords nil
  "a list of your YaTeX keywords")

(defconst ac-yatex-keywords 
  (sort 
   (append 
    ac-yatex-user-keywords
    ;; yatex.el
    section-table user-section-table tmp-section-table
    YaTeX-math-begin-list
    YaTeX-math-gathering-list
    env-table user-env-table tmp-env-table
    fontsize-table user-fontsize-table tmp-fontsize-table
    singlecmd-table user-singlecmd-table tmp-singlecmd-table

    ;; yatexadd.el
    YaTeX:style-parameters-default 
    YaTeX:style-parameters-private 
    YaTeX:style-parameters-local
    ;YaTeX:documentstyles-default
    ;YaTeX:documentstyles-private
    ;YaTeX:documentstyles-local
    ;YaTeX:documentstyle-options-default
    ;YaTeX:documentstyle-options-private
    ;YaTeX:documentstyle-options-local
    YaTeX:documentclass-options-default
    YaTeX:documentclass-options-private
    YaTeX:documentclass-options-local
    YaTeX:documentclasses-default
    YaTeX:documentclasses-private
    YaTeX:documentclasses-local
    YaTeX:latex2e-named-color-alist
    YaTeX:latex2e-basic-color-alist
    YaTeX::usepackage-alist-default
    YaTeX::usepackage-alist-private
    YaTeX::usepackage-alist-local

    ;; yatexmth.el
    ;YaTeX-math-sign-alist
    ;YaTeX-greek-key-alist

    ;; yatexpkg.el
    ;YaTeX-package-alist-default
    ;YaTeX-package-graphics-driver-alist
    ;YaTeX-package-alist-private
    
    ;; yatexsec.el
    ;YaTeX-sectioning-level
    ) #'(lambda (a b) (> (length a) (length b)))))

(defvar ac-source-yatex
  '((candidates
     . (lambda ()
	 (all-completions ac-target ac-yatex-keywords))))
  "Source for YaTeX keywords.")

(defun ac-yatex-setup ()
  (setq ac-modes (append ac-modes '(yatex-mode)))
  (setq ac-sources
	'(ac-source-yatex 
	  ac-source-abbrev 
	  ac-source-words-in-buffer 
	  ))
  )

(add-hook 'yatex-mode-hook 'ac-yatex-setup)

(provide 'auto-complete-yatex)

ac-yatex-user-keywords へ setq すれば,自分のオリジナルマクロを補完できます.

簡単なマクロを書いてみると気付きますが,plain TeX の csname のリストがまだまだ足りない感じです.

;;; AutoComplete
;; http://dev.ariel-networks.com/Members/matsuyama/auto-complete
(setq ac-yatex-user-keywords
      (list "Lbox[]{}" "Rbox[]{}" "MARU{}" "maru{}" "RN{}" "Hline"
	    "makeatletter" "makeatother"
	    "setbox" "lower" "raise" "llap" "rlap"
	    "hbox{}" "mbox{}" "fbox{}" "parbox[]{}"
	    "hfil" "hfill" "hss" "hskip" "hspace*{}"
	    "vfil" "vfill" "vss" "vskip" "vspace*{}"
	    "baselineskip" "onelineskip" "halflineskip"
	    ))
(when (require 'auto-complete nil t)
  (require 'auto-complete-yatex)
  )
(global-auto-complete-mode t)
;; Maybe default-enable-multibyte-characters is t by default
(setq default-enable-multibyte-characters t)

;; (set-face-background 'ac-menu-face "lightgray")
;; (set-face-underline 'ac-menu-face "darkgray")
(set-face-background 'ac-completion-face "lightgray")
(set-face-underline 'ac-candidate-face "darkgray")
(set-face-background 'ac-selection-face "steelblue")
;; (define-key ac-complete-mode-map "\t" 'ac-expand)
;; (define-key ac-complete-mode-map "\r" 'ac-complete)
;; (define-key ac-complete-mode-map "\M-n" 'ac-next)
;; (define-key ac-complete-mode-map "\M-p" 'ac-previous)
(setq ac-auto-start 2)
;; (setq ac-dwim t)

;; 部分マッチを実現してみた
;; http://d.hatena.ne.jp/rubikitch/20081109/autocomplete
(defvar ac-chars "0-9a-zA-Z@\\?!_-");; "0-9a-zA-Z\\?!_-"
(defun ac-candidate-substrings-in-buffer ()
  "partial dabbrev for auto-complete."
  (when (> (length ac-target) 1)
    (let ((inhibit-quit nil)            ;for debug
          (i 0)
          candidate
          candidates
          (regexp (concat (regexp-quote ac-target) "[" ac-chars "]*\\b")))
      (save-excursion
        ;; search backward
        (goto-char ac-point)
        (while (and (< i ac-candidate-max)
                    (re-search-backward regexp nil t))
          (skip-chars-backward ac-chars)
          (setq candidate (buffer-substring-no-properties
                           (point)
                           (match-end 0)))
          (when (and (>= (length candidate) 3)
                     (not (member candidate candidates)))
            (setq candidates (cons candidate candidates))
            (setq i (1+ i))))
        ;; search backward
        (goto-char (+ ac-point (length ac-target)))
        (while (and (< i ac-candidate-max)
                    (re-search-forward regexp nil t))
          (goto-char (match-beginning 0))
          (skip-chars-backward ac-chars)
          (setq candidate (buffer-substring-no-properties
                           (point)
                           (match-end 0)))
          (goto-char (match-end 0))
          (when (and (>= (length candidate) 3)
                     (not (member candidate candidates)))
            (setq candidates (cons candidate candidates))
            (setq i (1+ i))))
        (nreverse candidates)))))

(setq ac-source-substrings-in-buffer
      '((candidates  . ac-candidate-substrings-in-buffer)
        (limit . 4)))