2009-08-11 [長年日記]
_ [Emacs] auto-completeのac-candidate-words-in-bufferを改良
Emacsでauto-completeを使っているのだが、小文字で入力しているときに大文字の補完候補があり、それが補完されると打ち直すことになって不便。また、数字の列が補完されるのも困る。そこでac-candidate-words-in-bufferを改良してac-candidate-words-in-buffer+として使っている。
(defun ac-candidate-words-in-buffer+ () "Default implemention for `ac-candidate-function'." (if (> (length ac-prefix) 0) (let ((i 0) (case-fold-search nil) candidate candidates (regexp (concat "\\b" (regexp-quote ac-prefix) "\\(\\s_\\|\\sw\\)*\\b"))) (save-excursion ;; search backward (goto-char ac-point) (while (and (< i ac-limit) (re-search-backward regexp nil t)) (setq candidate (match-string-no-properties 0)) (unless (or (member candidate candidates) (< (length candidate) 3) (string-match "\\(^[+-.0-9]+$\\)\\|\\([+-.0-9][+-.0-9][+-.0-9]+$\\)\\|\\(^[+-.0-9][+-.0-9][+-.0-9]+\\)" candidate)) (push candidate candidates) (setq i (1+ i)))) ;; search backward (goto-char (+ ac-point (length ac-prefix))) (while (and (< i ac-limit) (re-search-forward regexp nil t)) (setq candidate (match-string-no-properties 0)) (unless (or (member candidate candidates) (< (length candidate) 3) (string-match "\\(^[+-.0-9]+$\\)\\|\\([+-.0-9][+-.0-9][+-.0-9]+$\\)\\|\\(^[+-.0-9][+-.0-9][+-.0-9]+\\)" candidate)) (push candidate candidates) (setq i (1+ i)))) (nreverse candidates))))) (setq ac-source-words-in-buffer '((candidates . ac-candidate-words-in-buffer+)))
case-fold-searchをnilに設定することで大文字、小文字の違いでマッチしないようにしている。2文字以下の文字列と、数値を表すような文字列を除くようにもした。
2009-08-13 [長年日記]
_ [Linux] gnuplot 保存
gnuplot で打ち込んだコマンドを保存するには
save 'result.txt'
次のようにして同じ作業を再び実行できる。
load 'result.txt'
_ [Emacs] auto-complete-yasnippet.el
complete-readingを使ったyasnippetをauto-complete-yasnippet.elを使ってauto-completeから呼び出すとエラーが起こる。回避するにはac-source-yasnippetのactionを変更して、yas/expandが呼ばれる前にac-abortすればよい。
(defvar ac-source-yasnippet '((candidates . ac-yasnippet-candidate) (action . (lambda () (ac-abort) (yas/expand))) ;; (action . yas/expand) (limit . 3) (candidate-face . ac-yasnippet-candidate-face) (selection-face . ac-yasnippet-selection-face)) "Source for Yasnippet.")
2009-08-17 [長年日記]
_ [Emacs] yasnippet のインデント
yasnippetで補完入力するときにインデントを自動でやってほしい。方法としては
(add-hook 'yas/after-exit-snippet-hook '(lambda () (indent-region yas/snippet-beg yas/snippet-end)))
のように補完後に実行されるhookにインデントを実行する関数を設定すれば良いとwikiに書いてある。ただ、こうするとundoしたときにうまく動かない。これは非常に不便なのでwikiを調べているとWishListに「auto indent like $> of smart-snippet.」という部分に線が引かれている。どうもyasnippet.elを調べると「$>」が実装されているようなので使ってみると、うまくインデントが行われた。たとえば、ruby-modeで使う「do ... end」の補完は次のようにすれば良い。
#name : do ... end # -- $>do $>$0 $>end
2009-08-21 [長年日記]
_ [Emacs] imenu for yatex
yatex-modeのときにimenuを使って見出し一覧を取り出すための関数を作った。
(defun YaTeX-create-imenu-index () (let ((index) (pattern (concat YaTeX-ec-regexp "\\(" YaTeX-sectioning-regexp "\\)\\*?{\\(.*\\)}"))) (goto-char (point-min)) (while (re-search-forward pattern (point-max) t) (push (cons (concat (match-string-no-properties 1) " - " (match-string-no-properties 6)) (match-beginning 0)) index)) (nreverse index)))
あとはフックでimenu-create-index-functionに設定する。which-funcを使っている場合はで見出しを表示できるようにwhich-func-modesにyatex-modeを追加する。
(add-hook 'yatex-mode-hook '(lambda () (setq imenu-create-index-function 'YaTeX-create-imenu-index))) (setq which-func-modes (append which-func-modes '(yatex-mode)))
2009-08-22 [長年日記]
_ [Emacs] htmlize
Emacsの場合、ソースコードのサンプルに色をつけるにはhtmlizeを使えば良いということで使ってみたのだが一つのhtmlとして出力されてしまう。ソースコード部分だけで良いので、preタグの部分だけを抜き出すような関数をhtmlize-after-hookに設定することにした。
;; htmlize (setq htmlize-output-type 'inline-css) (autoload 'htmlize-region "htmlize" nil t) (autoload 'htmlize-buffer "htmlize" nil t) (setq htmlize-after-hook '(htmlize-cut-header-and-footer)) (defun htmlize-cut-header-and-footer () (save-excursion (let ((pt-start) (str)) (goto-char (point-min)) (search-forward "<body ") (setq pt-start (point)) (skip-chars-forward "^>") (setq str (buffer-substring pt-start (point))) (search-forward "<pre") (insert " " str) (search-backward "<") (delete-region (point-min) (point)) (goto-char (point-max)) (search-backward "</pre>") (end-of-line) (delete-region (point) (point-max)))))
_ [Emacs] htmlize-regionの結果をkill-ringに送る
htmlize-regionの結果をkill-ringにコピーして、もとのバッファは削除するようなコマンドを作った。
(defun htmlize-region-to-kill-ring () (interactive) (if (and transient-mark-mode mark-active) (with-current-buffer (htmlize-region (region-beginning) (region-end)) (kill-region (point-min) (point-max)) (kill-buffer) (message "Put html pre tag to kill-ring")) (message "Mark not active")))
2009-08-26 [長年日記]
_ [Emacs] anything-quit-and-find-fileの改良
anythingを実行してC-x C-fとするとanything-quit-and-find-fileが実行される。このときに、選択されていた候補がファイルであればそのディレクトリからfind-fileを実行するように改良した。
(defun anything-quit-and-find-file () "Drop into `find-file' from `anything' like `iswitchb-find-file'. This command is a simple example of `anything-run-after-quit'." (interactive) (let ((filename (expand-file-name (anything-get-selection)))) (if (not (file-exists-p filename)) (setq filename nil)) (anything-run-after-quit '(lambda (f) (if f (let ((default-directory (file-name-directory f))) (call-interactively 'find-file)) (call-interactively 'find-file))) filename)))
2009-08-28 [長年日記]
_ [tDiary][Emacs] tdiary-modeの保存するディレクトリを変更する
tdiary-modeでは投稿すると同時に指定したディレクトリにファイルを保存するのだが、日記の数が多くなるとそのディレクトリに数多くのファイルがたまり不便。そこで年毎に分けることにした。変数を指定するだけではできないので、tdiary-post-textを変更する。
(defun tdiary-post-text () (let* ((dirname (expand-file-name (substring tdiary-date 0 4) (expand-file-name tdiary-diary-name (or tdiary-text-directory (expand-file-name "~/"))))) (filename (expand-file-name (concat tdiary-date tdiary-text-suffix) dirname))) (unless (file-directory-p dirname) (make-directory dirname t) (set-file-modes dirname tdiary-text-directory-mode)) ;; If buffer-file-name is tdiary-text-directory/tdiary-diary-name/yyyy/yyyymmdd.td ;; do nothing. (unless (equal filename buffer-file-name) (cond ((equal tdiary-edit-mode "replace") (write-region (point-min) (point-max) filename)) ((equal tdiary-edit-mode "append") (write-region (point-min) (point-max) filename t))))))
2009-08-29 [長年日記]
_ [Emacs][Linux] xdg-open を start-process から呼び出す
Ubuntu 9.04 には xdg-open という関連づけされたアプリケーションでファイルを開くコマンド(中身はシェルスクリプト)がある。gnome の場合は xdg-open の中で gnome-open が使われている。これを Emacs から非同期プロセスとして起動したかったのだが、
(start-process "xdg-proc-name" nil "xdg-open" "tmp.png")
としてもうまくいかなかった(emacs-snapshot、emacs22のどちらでも)。調べてみると下のように process-connection-type を nil にするとうまくいくようだ。
(defun xdg-open-file (filename) (interactive "fFile to open: ") (let ((process-connection-type nil)) (start-process "xdg-open-proc" nil "xdg-open" filename)))
これは、xdg-open の場合だけれど gnome-open でも同じようにすることができる。