Skip to content

Commit

Permalink
add cperl-reindent
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Jan 27, 2008
1 parent 6c90996 commit db9aad9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
8 changes: 3 additions & 5 deletions _local/cperl-extras.el
Expand Up @@ -7,6 +7,7 @@
(require 'cperl-misc)
(require 'cperl-project)
(require 'cperl-moose)
(require 'cperl-reindent)
(provide 'cperl-extras)

(add-hook 'cperl-mode-hook
Expand All @@ -20,16 +21,13 @@
(local-set-key "\C-cT" 'find-tests)
(local-set-key "\C-cw" 'swap-strict-and-moose)
(local-set-key "\C-c\C-f" 'ifind-perl-project-file)
; some fucktard overwrites this binding. fuckers.
(local-set-key "\C-c\C-p" 'ifind-perl-projects)
(local-set-key "\C-c510" 'kill-5.10)
(local-set-key "\C-cr" 'cperl-repl)))
(local-set-key "\C-cr" 'cperl-repl)
(local-set-key (quote [C-tab]) (quote cperl-reindent-hash))))

(add-hook 'tt-mode-hook
(lambda ()
(local-set-key "\C-c\C-f" 'ifind-perl-project-file)))

(global-set-key "\C-c\C-p" 'ifind-perl-projects)



78 changes: 78 additions & 0 deletions _local/cperl-reindent.el
@@ -0,0 +1,78 @@
(defun look-for-opening-without-close nil
"Look backwards from the point for an unbalanced opening brace.
For example, in `{ foo => sub {}, bar => baz X', we'll match the
leading `{'. Returns the position of the match, or nil if there
is no match."
;; TODO, if point is on or after a )}] character, move back one and
;; do this stuff; it only seems right.
(let ((state 0) pos)
(save-excursion
(while (>= state 0)
(if (not (re-search-backward
(format "\\(%s\\)\\|\\(%s\\)"
(regexp-opt (list "[" "(" "{")) ; opening
(regexp-opt (list "]" ")" "}"))) ; closing
nil t))

; no match, break out of loop
(setq state -1)

(if (match-string-no-properties 2) ; opening or closing?
(progn (setq state (+ state 1)))
(setq state (- state 1))
(match-beginning 0)
(setq pos (point))))) pos)))

(defun narrow-to-sexp-area nil
"Narrow from the point to the nearest unmatched opening thingie"
(interactive)
(let ((start (+ 1 (look-for-opening-without-close))) (end (point)))
(narrow-to-region start end)))

(defun cperl-reindent-eol nil
(save-excursion (end-of-line) (point)))

(defun cperl-reindent-bol nil
(save-excursion (beginning-of-line) (point)))

(defvar cperl-reindent-hash-key-regex
"[^[:space:]][[:space:]]*=>")

(defun cperl-reindent-hash-target nil
"What column should be = in `=>' be at?"
(let ((ret 0))
(ignore-errors
(while t
(beginning-of-line)
(let ((offset (point)))
(re-search-forward cperl-reindent-hash-key-regex)
(setq ret (max ret (+ 1 (- (match-beginning 0) offset)))))
(next-line)))
ret))

(defun cperl-reindent-hash nil
"Inside an `sexp' that contains pairs, reformat the pairs correctly."
(interactive)
(save-restriction
(narrow-to-sexp-area)
(save-excursion
(goto-char (point-min))
(let ((target (+ 1 (cperl-reindent-hash-target))))
(goto-char (point-min))
(ignore-errors
(while t
(re-search-forward cperl-reindent-hash-key-regex)
(let* ((bol (cperl-reindent-bol))
(cur (+ 1 (match-beginning 0)))
(col (- cur bol))
(spaces (- target col)))
(goto-char cur)
(if (looking-at "[[:space:]]+")
(replace-match "" nil t))
(dotimes (i spaces) (insert " "))
(re-search-forward "=>")
(if (looking-at "[[:space:]]*")
(replace-match " " nil t))
(end-of-line))))))))

(provide 'cperl-reindent)

0 comments on commit db9aad9

Please sign in to comment.