| | 27 | |
| | 28 | ;; cperl-mode初回起動時に、モジュール名を全て詰め込んだバッファを作成し、 |
| | 29 | ;; dabbrevでモジュール名を補完できるようにする。 |
| | 30 | ;; http://subtech.g.hatena.ne.jp/antipop/20070917/1190009355 |
| | 31 | (add-hook 'cperl-mode-hook |
| | 32 | (lambda () |
| | 33 | (let ((buffer nil) (buffer-name "*PerlModules*")) |
| | 34 | (unless (get-buffer buffer-name) |
| | 35 | (setq buffer (generate-new-buffer buffer-name)) |
| | 36 | (save-current-buffer |
| | 37 | (shell-command |
| | 38 | "find `perl -e 'print join(q{ }, @INC);'` -name '*.pm' -type f | xargs egrep -h -o 'package [a-zA-Z0-9:]+;' | perl -nle 's/package\s+(.+);/$1/; print' | sort | uniq" |
| | 39 | buffer) |
| | 40 | (set-buffer buffer) |
| | 41 | (delete-window)))))) |
| | 42 | |
| | 43 | ;; use文を、C-c C-mで自動挿入。 |
| | 44 | ;; http://subtech.g.hatena.ne.jp/antipop/20070917/1189962499 |
| | 45 | (add-hook 'cperl-mode-hook |
| | 46 | (lambda () |
| | 47 | (local-set-key (kbd "\C-c \C-m") 'perl-insert-use-statement))) |
| | 48 | |
| | 49 | (defun perl-insert-use-statement (current-point) |
| | 50 | "use statement auto-insertion." |
| | 51 | (interactive "d") |
| | 52 | (insert-use-statement |
| | 53 | (detect-module-name current-point) |
| | 54 | (detect-insert-point))) |
| | 55 | |
| | 56 | (defun insert-use-statement (module-name insert-point) |
| | 57 | (save-excursion |
| | 58 | (goto-char insert-point) |
| | 59 | (insert (concat "\nuse " module-name ";")))) |
| | 60 | |
| | 61 | (defun detect-insert-point () |
| | 62 | (save-excursion |
| | 63 | (if (re-search-backward "use .+;" 1 t) |
| | 64 | (match-end 0) |
| | 65 | (progn |
| | 66 | (string-match "^$" (buffer-string)) |
| | 67 | (match-end 0))))) |
| | 68 | |
| | 69 | (defun detect-module-name (current-point) |
| | 70 | (let ((str (save-excursion |
| | 71 | (buffer-substring |
| | 72 | current-point |
| | 73 | (progn (beginning-of-line) (point)))))) |
| | 74 | (if (string-match "\\([[:alnum:]-_:]+\\)$" str) |
| | 75 | (match-string 1 str) |
| | 76 | (error "Module name not found")))) |