reddit / emacs

Newbie. Trying to do first defun, need some help.

Status-Detective-783 · 2022-03-07 09:06
EDIT: I don't know why I was having a problem, but my code works just fine as it is. There's no problem here, but I'll leave this post up as an example! ____ I want `M-RET`(aka `org-meta-return`) to call `org-insert-heading-after-current` instead of `org-insert-heading`. To do this, I copied the the `(defun org-meta-return...)` from org.el to my init.el, and made some small changes. But it doesn't show up as an `M-x` command and it doesn't show up in the help system. ;; Override for org-meta-return to use org-insert-heading-after-current instead ;; of org-insert-heading ;; I just copied org-meta-return and made some changes to the help text and the function call. (defun dls-org-meta-return (&optional arg) "MY SPECIAL OVERRIDE. Insert a new heading or wrap a region in a table. Calls `org-insert-heading-after-current' [INSTEAD OF ORG-INSERT-HEADING], `org-insert-item' or `org-table-wrap-region', depending on context. When called with an argument, unconditionally call `org-insert-heading'." (interactive "P") (org-check-before-invisible-edit 'insert) (or (run-hook-with-args-until-success 'org-metareturn-hook) (call-interactively (cond (arg #'org-insert-heading-after-current) ((org-at-table-p) #'org-table-wrap-region) ((org-in-item-p) #'org-insert-item) (t #'org-insert-heading))))) I would like help getting this to work, or at least understanding why "it doesn't work that way". (I know I can just call `C-u C-RET` to accomplish the same thing, but I'm learning elisp and I did the above work first, so I'd like to know why it doesn't work.)
flexibeast · 2022-03-07 09:24
You're not overriding `org-meta-return` - you've created an entirely new interactive function called `dls-org-meta-return`. Which is good; actually 'overriding' a function either via advice or by redefining it should be a last resort. But now you need to rebind `M-RET` in org-mode to call `dls-org-meta-return`. As for why your function isn't showing up, have you asked Emacs to evaluate that form (i.e. your `defun`), either by doing `C-M-x` (`eval-defun`) with point inside it, by doing `M-x eval-buffer` in your `init.el`, or by restarting Emacs?
Status-Detective-783 · 2022-03-07 23:19
I don't know why I was having a problem, but my code works just fine as it is. Thanks again for the encouragement!