Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created July 13, 2010 04:43
Show Gist options
  • Save yaotti/473461 to your computer and use it in GitHub Desktop.
Save yaotti/473461 to your computer and use it in GitHub Desktop.
(defun insert-str-with-line-context (regexp inserting-str default-char)
(let ((ptr (point)))
(beginning-of-line)
(let ((str (buffer-substring (point) ptr)))
(goto-char ptr)
(if (string-match regexp str)
(insert inserting-str)
(insert default-char)))))
;; typing "4" inserts "$", not "4" if you wrote "my " or "my ("
(define-key cperl-mode-map (kbd "4")
(lambda () (interactive)
(insert-str-with-line-context "[ \t]*my[ ]+(?$" "$" "4")))
(define-key cperl-mode-map (kbd "8")
(lambda () (interactive)
(insert-str-with-line-context "[ \t]*my[ ]+$" "(" "8")))
;; typing ":" insert "::" after writing "package ***"
(define-key cperl-mode-map (kbd ":")
(lambda () (interactive)
(insert-str-with-line-context "^package" "::" ":")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment