Key Binding to Select Paragraph or Org Element

I used to rely on M-h to mark the structural element in Emacs; in text buffers, that’d be the paragraph, in org buffers, that was the whole outline item (when the cursor was in the heading line, at least).

Ever since I installed Emacs for Mac OS X which now is also on Emacs 28.1, this shortcut wouldn’t work for me anymore, because my Meta key is my left Command key, and Cmd-H is the macOS shortcut to hide the frontmost app.

I’m rather happy about Emacs for Mac OS X’s adherence to default macOS shortcuts.

So I looked for alternative key bindings.

My modal input scheme (Xah Fly Keys) has selection shortcuts on the number keys 6, 7, 8 and 9. But the one on 9 never made sense to me, so I now use that key to select the paragraph or org-element.

My own function that does the selection:

(defun ct/mark-paragraph ()
  "Mark the paragraph or structural element, depending on the mode."
  (interactive)
  (cond
   ((derived-mode-p 'org-mode)
    (org-mark-element))
   ((derived-mode-p 'web-mode)
     (web-mode-mark-and-expand))
   (t
    (mark-paragraph))))

This function is extensible to e.g. select an enclosing HTML element in web-mode or a function or type or other structural element in C code or Swift. I have yet to add these.

Update: Screw it, I just added the HTML variant. Didn’t even know it was bound to C-c RET until I looked for a suitable function.

I’m using derived-mode-p to test for both same modes, and derived modes; so e.g. (derived-mode-p 'outline-mode) will also return non-nil when visiting an org-mode buffer, since org-mode is derived from outline-mode. Got this tip from a post by Bozhidar Batsov.

A better name would maybe be ct/mark-paragraph-dwim because it’s context sensitive and does different things in different modes.

The key binding isn’t interesting, but here it is for completion:

(define-key xah-fly-command-map (kbd "9") #'ct/mark-paragraph)
;; was 'xah-select-text-in-quote