[SOLVED] Cursor jump to first line after last and to last from first (loop)


#1

I would like to know if there is a method, that if I am pressing the Down key (jumping down on lines) and I hit the last line, I want it to jump to the first one! Same, if I hit the first line by pressing the Up arrow, I want it to jump to the last line. Kind of like an endless cursor.


Keymap - action depending on what's under cursor
#2

You could certainly write an init.coffee script to accomplish this. You’d want to create a command that checks to see if the cursor position is on the last line of the buffer. If so, then it moves the cursor to [0,0]. Bind the command to down and it will take effect when you press the key while at the end of the buffer.


#3

Can you help me to find some sources for this or even help with the code? :slight_smile: I am pretty new in the topic, and so far I can change the cursor position to 0,0 on atom editor start by putting this code into init.coffee:

  atom.workspace.observeTextEditors (editor) ->
    editor.setCursorBufferPosition([0,0])

I also tried putting it into a function like this:

loopcursor = () ->      
  atom.workspace.observeTextEditors (editor) ->
    editor.setCursorBufferPosition([0,0])

And on keymap.cson

'atom-workspace':
  'down': 'loopcursor:down'

In this case, the down functionality totally broke in Atom, were not able to jump on lines anymore.

Can you tell me if I am on the right way at least? :slight_smile:


#4

The Atom documentation covers how to create a new command. A few points:

  1. Commands are conventionally named following the pattern <package>:<name>. For user-defined commands, it’s useful to have them all labeled like user:down or custom:down in order to make it clear that the command is coming from init.coffee.
  2. observeTextEditors() will return a bunch of editors, each of which will have a cursor position in memory. Your current code, if it worked, would set all editors’ cursors to the beginning of the buffer. You need to use atom.workspace.getActiveTextEditor().
  3. And then of course you need to check to see if the cursor is on the last line of the buffer before it jumps up.

#5

when I use getActiveTextEditor(), not even the code I wrote works. :confused:


#6

That’s because you’re not using atom.commands.add like is shown in the documentation that @DamnedScholar linked to. Atom commands aren’t functions that you put somewhere, they are objects that have to be registered with the Atom commands system so that they can be accessed via key maps.


#7

Don’t know if it is the easiest way, but I got it working! But I don’t know how to bind it to the down and up arrows. Every time I try, I break the default function (jumping on lines) and the command is not even run. :confused: (I hope it is okay to ask you guys so much, my JavaScript experience is literally about 10-20 hours… :smiley: So, so far, I am proud of myself :slight_smile: )

editor = atom.workspace.getActiveTextEditor()
atom.commands.add 'atom-text-editor', 'loop-cursor-bottom', ->
  if editor.getCursorBufferPosition().row == 0
    editor.moveToBottom()

atom.commands.add 'atom-text-editor', 'loop-cursor-top', ->
  if editor.getCursorBufferPosition().row == editor.getEofBufferPosition().row
    editor.moveToTop()

UPDATE: First of all, I had to cut the code into two commands, so I can bind them to different keys. But did not really solve the issue yet.

UPDATE2:

'body':
  'down' : 'loop-cursor-top'
  'up' : 'loop-cursor-bottom'

It makes the code work, but as said, the basic jumping functionality disappears. I guess I have to find a way, two bind two commands to one key?


#8

(ALMOST)

There are some problems, but almost there. It only works on the very first tab I open. If I change it to another one, or I open a new one, the commands does not run. Also if I have for example a right and a left pane, and I set the cursor into the right, the cursor jumps back to the left one when I press the down or up arrows.

It also triggers immidiately I jump to that line, so I cannot edit anything on the first and last lines. I need to run the code when the arrow keys were pressed at least two times on the given lines.

init.coffee

editor = atom.workspace.getActiveTextEditor()
atom.commands.add 'atom-text-editor', 'loop-cursor-bottom', ->
  editor.moveUp()
  if editor.getCursorBufferPosition().row == 0
    editor.moveToBottom()

atom.commands.add 'atom-text-editor', 'loop-cursor-top', ->
  editor.moveDown()
if editor.getCursorBufferPosition().row == editor.getEofBufferPosition().row
  editor.moveToTop()

keymap.cson

'atom-text-editor':
  'down' : 'loop-cursor-top'
  'up' : 'loop-cursor-bottom'

#9

This will take care of the “only works on the very first tab I open” problem (see below for an explanation). I also changed the names of the commands to be more in line with standard convention. Finally, I rearranged the if statement so that the command should not jump to the bottom when all you were trying to do was get to the topmost line :grinning:

init.coffee

atom.commands.add 'atom-text-editor', 'custom:loop-cursor-bottom', ->
  editor = atom.workspace.getActiveTextEditor()
  if editor.getCursorBufferPosition().row == 0
    editor.moveToBottom()
  else
    editor.moveUp()

atom.commands.add 'atom-text-editor', 'custom:loop-cursor-top', ->
  editor = atom.workspace.getActiveTextEditor()
  if editor.getCursorBufferPosition().row == editor.getEofBufferPosition().row
    editor.moveToTop()
  else
    editor.moveDown()

keymap.cson

'atom-text-editor':
  'down' : 'custom:loop-cursor-top'
  'up' : 'custom:loop-cursor-bottom'

The way you had the code before, you were storing the active text editor when the init.coffee script was loaded and executed, i.e. when Atom starts up. The way that I have it written here, the code now determines what the active text editor is when the command is executed.


#10

Thanks! Now only one issue left, and I hope it is done :innocent:

When typing, sometimes tips comes up, like in JS writing an l brings up three options for me, log, legal and lorem. The problem is that since the down key means to jump down a line, I cannot change on the tips. Is there a way, to very specifically tell Atom, that do the command only on the plain text editor, nowhere else, excluding the popup tips window also?


#11

There isn’t a way to do specifically what you’re asking (because it would require a whitelist) … but there are a couple options in the autocomplete-plus package settings that should help:

  1. Launch Atom
  2. Open the Settings View using Cmd+, on macOS or Ctrl+, on other platforms
  3. Click the Packages tab
  4. Search for “autocomplete-plus”
  5. Click the Settings button on the autocomplete-plus package card
  6. Find the settings for “File Blacklist” and “Scope Blacklist”

This will allow you to tell the autocomplete feature where to not show up.


#12

#Solved

I think I explained my problem badly, but I got helped by your comment anyway!

You have helped me a lot, and now I am finally finished. It wasn’t a big project probably, but as a total beginner(and I mean I had no idea about the 80% of the stuff you just wrote) after a day, I (we) did it. :slight_smile:

The final code:

init.coffee

atom.commands.add 'atom-text-editor', 'custom:loop-cursor-bottom', ->
  editor = atom.workspace.getActiveTextEditor()
  if editor.getCursorBufferPosition().row == 0
    editor.moveToBottom()
  else
    editor.moveUp()

atom.commands.add 'atom-text-editor', 'custom:loop-cursor-top', ->
  editor = atom.workspace.getActiveTextEditor()
  if editor.getCursorBufferPosition().row == editor.getEofBufferPosition().row
    editor.moveToTop()
  else
    editor.moveDown()

keymap.cson

'atom-text-editor:not(.autocomplete-active)':
  'down' : 'custom:loop-cursor-top'
  'up' : 'custom:loop-cursor-bottom'

And as far as I see, everything works just fine!

Thank you again! And have a very nice day, sir!