0

I am using beginning-of-buffer to the jump beginning of a buffer.

Is it possible to achive same behavior but to the first line under the import or from` section in the Python mode.

In this example I want to jump to 17th line. There could be multiple modules under from ... import ( and there could be also a new-line in between from ... and import... lines.

1:  #!/usr/bin/env python3
2: 
3:  import os
4:  import sys
5:  import textwrap
6:  import time
7:  from broker.utils.tools import log
8: 
9:  import zc.lockfile
10: from ipdb import launch_ipdb_on_exception
11: 
12: from broker.utils import (
13:     CacheType,
14:     StorageID,
15: )
16:
17: # <== jump here
18: 
19: def _main():
20:     from my_module.utils.tools import work
21:
22:     work()
2
  • What have you tried so far?
    – Dan
    Oct 26, 2021 at 15:51
  • I have not tried anything due to my lack of knowledge in lisp coding.
    – alper
    Oct 26, 2021 at 16:10

1 Answer 1

1

I am not aware of some existing function that does this. But there are many simple ways to achieve this; two commands you could use are:

EDIT in response to your first comment

So I guess after finding the last "import", you could just continue to look for the first empty line.

SECOND EDIT in response to your second comment

To exclude 'searching for/from' "import"s in 'the body' of the file, we can just limit the range for the search to "import", e.g. as follows:

(defun my-alternative-jump-to-end-of-imports ()
  (interactive)
  (goto-char (point-min))
  (let (end)
    (save-excursion
      (forward-line 3)
      (setq end (point)))
    (while (search-forward "import" end t)
      (save-excursion
        (forward-line 3)
        (setq end (point))))
    (while (and (not (eobp))
                (not (member (thing-at-point 'line t) '("" "\n"))))
      (forward-line))
    (if (bolp)
        (forward-line)
      (insert "\n\n"))))

You can play a little with the number of lines for searching forward. It is not perfect, but I guess it works fine for 99% of the cases.

END EDITS

I would suggest that you take some time to read a few chapters of An Introduction to Programming in Emacs Lisp (or read it right inside of Emacs) and study the above functions; it is not so much work, and Emacs becomes so much more fun to use.

8
  • Thank you, I will study the referenced book. If there is new-line in between the from and import lines it jumps into first new-line location. Would it be possible to pass over those new-lines as well? I updated my question based on that. Seems like complexity of the question increased :(
    – alper
    Oct 27, 2021 at 10:35
  • I've edited the answer in response to your comment Oct 27, 2021 at 12:05
  • Ah if there is something like from my_module.imports import connect at the bottom of page inside a module(def main(): ) it jumps below that instead of the top modules (I have updated my question according to that as well). I will find way to ignore that , other than it works great
    – alper
    Oct 27, 2021 at 12:30
  • Okay... I've updated the answer Oct 27, 2021 at 14:21
  • 1
    Thank you, I appreciated your patient help
    – alper
    Oct 27, 2021 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.