tamzinblake / js3-mode

A chimeric fork of js2-mode and js-mode
GNU General Public License v3.0
181 stars 13 forks source link

Command to add current word to list of globals #61

Closed mbriggs closed 10 years ago

mbriggs commented 12 years ago

This function has been sitting my my "random defuns" file for awhile, figured it may be a nice addition to js3 mode (if you like it, just tell me where to put it and what to bind it to and I'll open a pull request)

What this will do is add the word-at-point to the list of /* global */ at the top of the file. If that directive is not there, it will add it. If the directive spans multiple lines, it will add it to the last line.

Still not that great at elisp, so if I am doing anything dumb, would be glad to hear that too :)

(defun add-to-js-globals ()
  (interactive)
  (let ((var (word-at-point)))
    (save-excursion
      (beginning-of-buffer)
      (when (not (string-match "^/\\* global " (current-line)))
          (newline)
          (previous-line)
          (insert "/* global */"))
      (while (not (string-match "*/" (current-line)))
        (next-line))
      (end-of-line)
      (delete-char -2)
      (insert (concat var " */")))))
tamzinblake commented 12 years ago

It might be worth adding a check whether var already exists in js3-additional-externs so that it doesn't get added twice.

This looks cool. Bind it to whatever works for you, following the usual Emacs conventions. If you do a pull request, please add the code under /lib then rebuild.

tamzinblake commented 12 years ago

I added a function for this. I'm holding off on choosing a keybinding, but feel free to suggest one or submit a pull request.

tamzinblake commented 12 years ago

@mbriggs By the way, (beginning-of-buffer), (previous-line), and (next-line) are intended for interactive use only (not in lisp code), and (current-line) does not appear to be a function.

mbriggs commented 12 years ago

yeah, current-line was a utility function from some other random library (sorry about that). Is it bad practice to use interactive functions? They seemed more descriptive then things like (goto-char 0) and (forward-line -1)

tamzinblake commented 12 years ago

Those particular functions are labeled as "intended for interactive use only", and they give warnings to that effect when compiling.

tamzinblake commented 12 years ago

@mbriggs any feedback on keybinding / other concerns?

mbriggs commented 12 years ago

nope, i use evil-mode, so all my opinions on keybinds are horribly heretical :) better its chosen by a for-real emacs user

ozanmakes commented 11 years ago

It might be worth adding a check whether var already exists in js3-additional-externs so that it doesn't get added twice.

@thomblake Can you reconsider this? Sometimes it makes sense to declare a name as a global in the source even if I already have it in js3-additional-externs to make the linter happy and get rid of flymake warnings.

tamzinblake commented 11 years ago

@osener It should definitely not add vars that are already in the globals list, but it makes sense to track that separately from js3-additional-externs.

tamzinblake commented 10 years ago

Let me know if there's a problem with C-c C-g as a key binding