Skip to content

Instantly share code, notes, and snippets.

@syohex
Created March 7, 2012 02:04
Show Gist options
  • Save syohex/1990438 to your computer and use it in GitHub Desktop.
Save syohex/1990438 to your computer and use it in GitHub Desktop.
Insert use statement
;; insert 'use Module' which is at cursor.
(define-key cperl-mode-map (kbd "C-c C-m") 'cperl-insert-use-statement)
(defun cperl-insert-use-statement ()
"use statement auto-insertion."
(interactive)
(let ((module-name (cperl-word-at-point))
(insert-point (cperl-detect-insert-point)))
(save-excursion
(let ((use-statement (concat "\nuse " module-name ";")))
(if (not (search-backward use-statement nil t))
(progn
(goto-char insert-point)
(insert use-statement))
(error "'%s' is already imported" module-name))))))
(defun cperl-detect-insert-point ()
(save-excursion
(if (re-search-backward "use .+;" 1 t)
(match-end 0)
(progn
(string-match "^$" (buffer-string))
(match-end 0)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment