• Emacs: Navigation By Sections

    From Lawrence D'Oliveiro@21:1/5 to All on Sat Mar 23 05:49:04 2024
    Some of my earliest exposure to code written by others was on a DEC
    PDP-11 system. So I somehow picked up the habit of writing
    section-header comments that look like this:

    ;+
    ; Navigation by sections
    ;-

    Of course, this can be adapted to other languages that use different
    comment delimiters:

    #+
    # High-level stuff follows
    #-

    However, where the language allows block comments (like C), I still
    have this fondness for using those instead:

    /*
    Binary heaps

    See definition module for an explanation
    */

    So I wrote a pair of simple navigation commands that will match
    section-header comments written in all these different formats, and
    lets me jump quickly between them:

    (setq section-header-pattern "^\\(?:[\\#;]\\+\\|/\\*\\)$")
    (setq section-header-end-pattern "^\\(?:[\\#;]-\\|\\*/\\)$")
    ; Allows for: “#+\n...#-\n” (Python etc); “;+\n...;-\n” (Lisp); “/*\n...*/\n” (C).

    (defun prev-section ()
    (interactive)
    (beginning-of-line)
    (unless (re-search-backward section-header-pattern nil t)
    (goto-char (point-min))
    ) ; unless
    ) ; prev-section

    (defun next-section ()
    (interactive)
    (end-of-line)
    (cond
    ((re-search-forward section-header-pattern nil t)
    (beginning-of-line)
    )
    (t
    (goto-char (point-max))
    )
    ) ; cond
    ) ; next-section

    (global-set-key [kp-subtract] 'prev-section)
    (global-set-key [kp-add] 'next-section)

    Yeah, I bind them to the numeric-keypad “+” and “-” keys. Does anybody use those for anything else? ;)

    Do people writing code on DEC systems still use a convention like this?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)