Skip to content

Commit

Permalink
Some JS fixes for espresso-mode; thanks to dgoodlad.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Sep 9, 2009
1 parent 0a164f2 commit 9b39bcb
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 96 deletions.
209 changes: 128 additions & 81 deletions elpa-to-submit/paredit.el
@@ -1,9 +1,13 @@
;;; -*- Mode: Emacs-Lisp; outline-regexp: " \n;;;;+" -*-

;;;;;; Paredit: Parenthesis-Editing Minor Mode
;;;;;; Version 21
;;;;;; Version 22 (beta)

;;; Copyright (c) 2008, Taylor R. Campbell
;;; NOTE: THIS IS A BETA VERSION OF PAREDIT. USE AT YOUR OWN RISK.
;;; THIS FILE IS SUBJECT TO CHANGE, AND NOT SUITABLE FOR DISTRIBUTION
;;; BY PACKAGE MANAGERS SUCH AS APT, PKGSRC, MACPORTS, &C.

;;; Copyright (c) 2008, 2009, Taylor R. Campbell
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -34,7 +38,7 @@
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; This file is permanently stored at
;;; <http://mumble.net/~campbell/emacs/paredit-21.el>.
;;; <http://mumble.net/~campbell/emacs/paredit-22.el>.
;;;
;;; The currently released version of paredit is available at
;;; <http://mumble.net/~campbell/emacs/paredit.el>.
Expand Down Expand Up @@ -174,8 +178,8 @@

;;; This assumes Unix-style LF line endings.

(defconst paredit-version 21)
(defconst paredit-beta-p nil)
(defconst paredit-version 22)
(defconst paredit-beta-p t)

(eval-and-compile

Expand Down Expand Up @@ -232,8 +236,14 @@ Signal an error if no clause matches."
(defvar paredit-mode-map (make-sparse-keymap)
"Keymap for the paredit minor mode.")

;;;###autoload
(define-minor-mode paredit-mode
"Minor mode for pseudo-structurally editing Lisp code.
With a prefix argument, enable Paredit Mode even if there are
imbalanced parentheses in the buffer.
Paredit behaves badly if parentheses are imbalanced, so exercise
caution when forcing Paredit Mode to be enabled, and consider
fixing imbalanced parentheses instead.
\\<paredit-mode-map>"
:lighter " Paredit"
;; If we're enabling paredit-mode, the prefix to this code that
Expand Down Expand Up @@ -613,6 +623,11 @@ Deprecated: use `paredit-mode' instead."

;;;; Delimiter Insertion

(defvar paredit-space-delimiter-chars (list ?w ?_ ?\")
"Characters for which a space should be inserted between them
and a delimiter. Set this to (list ?\\\") as a buffer-local value
for non-lisp languages.")

(eval-and-compile
(defun paredit-conc-name (&rest strings)
(intern (apply 'concat strings)))
Expand Down Expand Up @@ -774,9 +789,9 @@ If such a comment exists, delete the comment (including all leading
;; close the string), do insert a space.
(and (not (if endp (eobp) (bobp)))
(memq (char-syntax (if endp (char-after) (char-before)))
(list ?w ?_ ?\"
(let ((matching (matching-paren delimiter)))
(and matching (char-syntax matching)))))))
(append paredit-space-delimiter-chars
(list (let ((matching (matching-paren delimiter)))
(and matching (char-syntax matching))))))))

(defun paredit-move-past-close-and-reindent (close)
(let ((open (paredit-missing-close)))
Expand Down Expand Up @@ -979,11 +994,35 @@ If the point is in a string or a comment, fill the paragraph instead,
(paredit-in-comment-p))
(fill-paragraph argument)
(save-excursion
(end-of-defun)
(beginning-of-defun)
(indent-sexp))))

;;;; Comment Insertion

;;; This is all a horrible, horrible hack, primarily for GNU Emacs 21,
;;; in which there is no `comment-or-uncomment-region'.

(autoload 'comment-forward "newcomment")
(autoload 'comment-normalize-vars "newcomment")
(autoload 'comment-region "newcomment")
(autoload 'comment-search-forward "newcomment")
(autoload 'uncomment-region "newcomment")

(defun paredit-initialize-comment-dwim ()
(require 'newcomment)
(if (not (fboundp 'comment-or-uncomment-region))
(defalias 'comment-or-uncomment-region
(lambda (beginning end &optional argument)
(interactive "*r\nP")
(if (save-excursion (goto-char beginning)
(comment-forward (point-max))
(<= end (point)))
(uncomment-region beginning end argument)
(comment-region beginning end argument)))))
(defalias 'paredit-initialize-comment-dwim 'comment-normalize-vars)
(comment-normalize-vars))

(defun paredit-comment-dwim (&optional argument)
"Call the Lisp comment command you want (Do What I Mean).
This is like `comment-dwim', but it is specialized for Lisp editing.
Expand Down Expand Up @@ -1012,38 +1051,22 @@ At the top level, where indentation is calculated to be at column 0,
(comment-kill (if (integerp argument) argument nil))
(comment-indent)))
(t (paredit-insert-comment))))

;;; This is all a horrible, horrible hack, primarily for GNU Emacs 21,
;;; in which there is no `comment-or-uncomment-region'.

(defun paredit-initialize-comment-dwim ()
(require 'newcomment)
(if (not (fboundp 'comment-or-uncomment-region))
(defalias 'comment-or-uncomment-region
(lambda (beginning end &optional argument)
(interactive "*r\nP")
(funcall (if (save-excursion (goto-char beginning)
(comment-forward (point-max))
(<= end (point)))
'uncomment-region
'comment-region)
beginning end argument))))
(defalias 'paredit-initialize-comment-dwim 'comment-normalize-vars)
(comment-normalize-vars))

(defun paredit-comment-on-line-p ()
"True if there is a comment on the line following point.
This is expected to be called only in `paredit-comment-dwim'; do not
call it elsewhere."
(save-excursion
(beginning-of-line)
(let ((comment-p nil))
;; Search forward for a comment beginning. If there is one, set
;; COMMENT-P to true; if not, it will be nil.
(while (progn (setq comment-p
(search-forward ";" (point-at-eol)
;; t -> no error
t))
(and comment-p
(or (paredit-in-string-p)
(paredit-in-char-p (1- (point))))))
(while (progn
(setq comment-p ;t -> no error
(comment-search-forward (point-at-eol) t))
(and comment-p
(or (paredit-in-string-p)
(paredit-in-char-p (1- (point))))))
(forward-char))
comment-p)))

Expand All @@ -1054,35 +1077,28 @@ At the top level, where indentation is calculated to be at column 0,
(code-before-p
(save-excursion (paredit-skip-whitespace nil (point-at-bol))
(not (bolp)))))
(if (and (bolp)
;; We have to use EQ 0 here and not ZEROP because ZEROP
;; signals an error if its argument is non-numeric, but
;; CALCULATE-LISP-INDENT may return nil.
(eq (let ((indent (calculate-lisp-indent)))
(if (consp indent)
(car indent)
indent))
0))
;; Top-level comment
(progn (if code-after-p (save-excursion (newline)))
(insert ";;; "))
(if code-after-p
;; Code comment
(progn (if code-before-p
;++ Why NEWLINE-AND-INDENT here and not just
;++ NEWLINE, or PAREDIT-NEWLINE?
(newline-and-indent))
(cond ((and (bolp)
(let ((indent
(let ((indent (calculate-lisp-indent)))
(if (consp indent) (car indent) indent))))
(and indent (zerop indent))))
;; Top-level comment
(if code-after-p (save-excursion (newline)))
(insert ";;; "))
((or code-after-p (not code-before-p))
;; Code comment
(if code-before-p (newline))
(lisp-indent-line)
(insert ";; ")
(if code-after-p
(save-excursion
(newline)
(lisp-indent-line)
(insert ";; ")
;; Move the following code. (NEWLINE-AND-INDENT will
;; delete whitespace after the comment, though, so use
;; NEWLINE & LISP-INDENT-LINE manually here.)
(save-excursion (newline)
(lisp-indent-line)))
;; Margin comment
(progn (indent-to comment-column
1) ; 1 -> force one leading space
(insert ?\; ))))))
(indent-sexp))))
(t
;; Margin comment
(indent-to comment-column 1) ; 1 -> force one leading space
(insert ?\; )))))

;;;; Character Deletion

Expand Down Expand Up @@ -1456,6 +1472,29 @@ With a numeric prefix argument N, do `kill-line' that many times."
;; nil nil parse-state)
)
(t parse-state)))

(defun paredit-copy-as-kill ()
"Save in the kill ring the region that `paredit-kill' would kill."
(interactive)
(save-excursion
(if (paredit-in-char-p)
(backward-char 2))
(let ((beginning (point))
(eol (point-at-eol)))
(let ((end-of-list-p (paredit-forward-sexps-to-kill beginning eol)))
(if end-of-list-p (progn (up-list) (backward-char)))
(copy-region-as-kill beginning
(cond (kill-whole-line
(or (save-excursion
(paredit-skip-whitespace t)
(and (not (eq (char-after) ?\; ))
(point)))
(point-at-eol)))
((and (not end-of-list-p)
(eq (point-at-eol) eol))
eol)
(t
(point))))))))

;;;; Cursor and Screen Movement

Expand Down Expand Up @@ -1671,25 +1710,33 @@ With a prefix argument N, kill only the following N S-expressions."
(defun paredit-raise-sexp (&optional n)
"Raise the following S-expression in a tree, deleting its siblings.
With a prefix argument N, raise the following N S-expressions. If N
is negative, raise the preceding N S-expressions."
is negative, raise the preceding N S-expressions.
If the point is on an S-expression, such as a string or a symbol, not
between them, that S-expression is considered to follow the point."
(interactive "p")
(paredit-lose-if-not-in-sexp 'paredit-raise-sexp)
;; Select the S-expressions we want to raise in a buffer substring.
(let* ((bound (save-excursion (forward-sexp n) (point)))
(sexps (if (and n (< n 0))
(buffer-substring bound
(paredit-point-at-sexp-end))
(buffer-substring (paredit-point-at-sexp-start)
bound))))
;; Move up to the list we're raising those S-expressions out of and
;; delete it.
(backward-up-list)
(delete-region (point) (save-excursion (forward-sexp) (point)))
(save-excursion (insert sexps)) ; Insert & reindent the sexps.
(save-excursion (let ((n (abs (or n 1))))
(while (> n 0)
(paredit-forward-and-indent)
(setq n (1- n)))))))
(save-excursion
(cond ((paredit-in-string-p)
(goto-char (car (paredit-string-start+end-points))))
((paredit-in-char-p)
(backward-sexp))
((paredit-in-comment-p)
(error "No S-expression to raise in comment.")))
;; Select the S-expressions we want to raise in a buffer substring.
(let* ((bound (save-excursion (forward-sexp n) (point)))
(sexps (if (and n (< n 0))
(buffer-substring bound
(paredit-point-at-sexp-end))
(buffer-substring (paredit-point-at-sexp-start)
bound))))
;; Move up to the list we're raising those S-expressions out of and
;; delete it.
(backward-up-list)
(delete-region (point) (save-excursion (forward-sexp) (point)))
(save-excursion (insert sexps)) ; Insert & reindent the sexps.
(save-excursion (let ((n (abs (or n 1))))
(while (> n 0)
(paredit-forward-and-indent)
(setq n (1- n))))))))

(defun paredit-convolute-sexp (&optional n)
"Convolute S-expressions.
Expand Down Expand Up @@ -1810,7 +1857,7 @@ If in a string, move the opening double-quote forward by one
Automatically reindent the newly barfed S-expression with respect to
its new enclosing form."
(interactive)
(paredit-lose-if-not-in-sexp 'paredit-forward-slurp-sexp)
(paredit-lose-if-not-in-sexp 'paredit-forward-barf-sexp)
(save-excursion
(up-list) ; Up to the end of the list to
(let ((close (char-before))) ; save and delete the closing
Expand Down Expand Up @@ -1882,7 +1929,7 @@ If in a string, move the opening double-quote backward by one
Automatically reindent the barfed S-expression and the form from which
it was barfed."
(interactive)
(paredit-lose-if-not-in-sexp 'paredit-forward-slurp-sexp)
(paredit-lose-if-not-in-sexp 'paredit-backward-barf-sexp)
(save-excursion
(backward-up-list)
(let ((open (char-after)))
Expand Down
5 changes: 2 additions & 3 deletions starter-kit-defuns.el
Expand Up @@ -202,9 +202,8 @@ Symbols matching the text at point are put first in the completion list."

(defun esk-paredit-nonlisp ()
"Turn on paredit mode for non-lisps."
(set (make-local-variable 'paredit-space-for-delimiter-predicate)
(lambda (endp delimiter)
(equal (char-syntax (char-before)) ?\")))
(set (make-local-variable 'paredit-space-delimiter-chars)
(list ?\"))
(paredit-mode 1))

(defun toggle-fullscreen ()
Expand Down
23 changes: 11 additions & 12 deletions starter-kit-js.el
Expand Up @@ -2,16 +2,6 @@
;;
;; Part of the Emacs Starter Kit

(font-lock-add-keywords
'espresso-mode `(("\\(function *\\)("
(0 (progn (compose-region (match-beginning 1) (match-end 1)
"ƒ")
nil)))))

(font-lock-add-keywords 'espresso-mode
'(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):"
1 font-lock-warning-face t)))

(autoload 'espresso-mode "espresso" "Start espresso-mode" t)
(add-to-list 'auto-mode-alist '("\\.js$" . espresso-mode))
(add-to-list 'auto-mode-alist '("\\.json$" . espresso-mode))
Expand All @@ -21,10 +11,19 @@
(add-hook 'espresso-mode-hook 'idle-highlight)
(setq espresso-indent-level 2)

;; espresso's insert-and-indent doesn't play nicely with pretty-lambda
(eval-after-load 'espresso
'(progn (define-key espresso-mode-map "{" 'paredit-open-brace)
(define-key espresso-mode-map "}" 'paredit-close-brace-and-newline)))
(define-key espresso-mode-map "}" 'paredit-close-brace-and-newline)
;; fixes problem with pretty function font-lock
(define-key espresso-mode-map (kbd ",") 'self-insert-command)
(font-lock-add-keywords 'espresso-mode
'(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):"
1 font-lock-warning-face t)))
(font-lock-add-keywords
'espresso-mode `(("\\(function *\\)("
(0 (progn (compose-region (match-beginning 1)
(match-end 1) "ƒ")
nil)))))))

(provide 'starter-kit-js)
;;; starter-kit-js.el ends here

0 comments on commit 9b39bcb

Please sign in to comment.