Elisp function throws error only when called while init.el is open in buffer - debugging

I am using Emacs, and wrote a custom function in init.el to make it easier for me to create Anki flashcards upon press of a keybind. This specific function is used to retrieve two values (using Anki-Connect) and let me choose them from a list (the function is based on behaviour found in anki-editor.
In order to do that, I declared a few variables outside the function, so that I can later use their values in a org-capture-template. They are set to nil at the start because I check somewhere else if they have already been set and otherwise call the below function first.
My solution works, but only as long as init.el (or Emacs.org, from which it is compiled by tangle) is closed. If I call the function while init.el is open, I get the following error: "Wrong number of arguments: (3 . 3), 2" - I do not understand why that happens.
How can I fix this problem?
My code looks like this:
(defvar my-anki-deck nil)
(defvar my-anki-notetype nil)
(defvar my-anki-fields nil)
(defun my-anki-set-deck-and-notetype ()
(interactive)
(setq my-anki-deck (completing-read "Choose a deck: "
(sort (anki-editor-deck-names)
#'string-lessp)))
(setq my-anki-notetype (completing-read "Choose a note type: "
(sort (anki-editor-note-types)
#'string-lessp)))
(setq my-anki-fields (progn
(anki-editor--anki-connect-invoke-result
"modelFieldNames"
`((modelName . ,my-anki-notetype))))))
Below is the detailed error message (I tried to format it nicely, this is the best I was able to do):
Debugger entered--Lisp error: (wrong-number-of-arguments (3 . 3) 2)
request--curl-callback(# "finished\n")
anki-editor--anki-connect-invoke("deckNames")
anki-editor-deck-names()
(sort (anki-editor-deck-names) #'string-lessp)
(completing-read "Choose a deck: " (sort (anki-editor-deck-names) #'string-lessp))
(setq my-anki-deck (completing-read "Choose a deck: " (sort (anki-editor-deck-names) #'string-lessp)))
my-anki-set-deck-and-notetype()
funcall-interactively(my-anki-set-deck-and-notetype)
command-execute(my-anki-set-deck-and-notetype record)
counsel-M-x-action("my-anki-set-deck-and-notetype")
ivy-call()
ivy-read("M-x " [evil-collection-magit-toggle-text-minor-mode table--measure-max-width vc-src-responsible-p lsp:omnisharp-run-tests-in-class-request-column nexti tramp-completion-handle-file-name-all-completions term-do-line-wrapping lsp-watch-kind? speedbar-check-vc-this-line access-label curr-btn magit-revision-filter-files-on-follow treemacs-dom-node->set-position! calcFunc-rgrade term-handle-exit &error rng-parse-validate-file magit-xref-insert-button speedbar-file-regexp gnus-agent-group-covered-p evil-collection-org-maps edebug-mode-on-hook message-expand-name-databases C-i autoload-find-file c-make-ml-string-opener-re-function gnus-try-warping-via-registry magit-list-modified-modules pullreq_label:pullreq gnus-server-compact-server :odt-styles-file gdb-thread-number :mac org-table-get-range vc-bzr-shelve-menu rng-match-infer-start-tag-namespace Above nnimap-status-message gnus-summary-limit-to-marks treemacs--get-label-of nnmail-message-id-cache-file treemacs-python-executable article-fill-long-lines smime-buffer-as-string-region lsp:set-file-operation-pattern-options? diredfl-symlink Parchment c-default-value-sentence-end rg-elisp-font-lock-keywords posframe-poshandler-frame-bottom-left-corner ...] :predicate #f(compiled-function (sym) #<bytecode -0x11c32b217e0c370d>) :require-match t :history counsel-M-x-history :action counsel-M-x-action :keymap (keymap (67108908 . counsel--info-lookup-symbol) (67108910 . counsel-find-symbol)) :initial-input nil :caller counsel-M-x)
counsel-M-x()
funcall-interactively(counsel-M-x)
command-execute(counsel-M-x)

Related

Elisp - How to run a shell command and make buffer to be in markdown-mode?

(defun jira-view-git-branch ()
(interactive)
(markdown-mode)
(shell-command (format "./jira-view.sh &")))
So how to make the output buffer to be in markdown mode?
I tried the following
(defun jira-view-git-branch ()
(interactive)
(with-output-to-temp-buffer "*jira*"
(shell-command (format "./jira-view.sh &") "*jira*" "*Messages*")
(pop-to-buffer "*jira*"))
(with-current-buffer "*jira*"
(markdown-mode)))
but got this in *Messages*
error in process filter: read-from-minibuffer: Wrong type argument: markerp, nil
error in process filter: Wrong type argument: markerp, nil
Without knowing what your shell command jira-view.sh does exactly, I find it hard to come up with a good solution for this.
At least, the following should give you some pointers:
(defun jira-md (buffer)
(interactive "Bbuffer name: ")
(let ((b (get-buffer-create buffer)))
(switch-to-buffer b)
(markdown-mode)
(insert (shell-command-to-string "echo '# title'"))))
You can ask for a (possibly not yet existing) buffer when calling this function by having B be the first character in the argument to interactive
Once you have a buffer name, you can switch to that buffer and then set the major mode.
You could also make the setting of major mode optional by first examining if the major mode is not already set to markdown-mode. Something like:
(unless (eq major-mode 'markdown-mode)
(markdown-mode))

edebug-trace ceased to exist

In Emacs 25.2, suddenly the variable edebug-trace ceased to exist. When I set it with setq, it has no effect (the trace buffer does not appear). What could have happened and how can I fix it?
In the meanwhile, is there another way to know which function gets called when I click on an org-mode link
You could use trace.el to trace all org functions like so (I suggest not evaluating this until you're ready to click the link).
(mapatoms
(lambda (sym)
(and (fboundp sym)
(string-prefix-p "org-" (symbol-name sym))
(trace-function-foreground sym))))
Afterwards, you can remove the traces with:
M-x untrace-all RET
Edit: We could also convert that into a command ala elp-instrument-package:
(defun my-trace-package (prefix)
"Trace all functions which start with PREFIX.
For example, to trace all ELP functions, do the following:
\\[my-trace-package] RET elp- RET"
(interactive ;; derived from `elp-instrument-package'.
(list (completing-read "Prefix of package to trace: "
obarray 'my-traceable-p)))
(if (zerop (length prefix))
(error "Tracing all Emacs functions would render Emacs unusable"))
(mapc (lambda (name)
(trace-function-foreground (intern name)))
(all-completions prefix obarray 'my-traceable-p))
(message "Use %s to cease tracing."
(substitute-command-keys "\\[untrace-all]")))
(defun my-traceable-p (fun)
"Predicate for `my-trace-package'."
(or (functionp fun) (macrop fun)))

How to add and remove function to after-save-hook for specific major mode?

I wrote a simple major mode for a configuration file.
I would like to check the syntax upon saving.
How to make sure the my-check-syntax is added to after-save-hook iff it is under my-config-mode.
(defun my-check-syntax ()
;; code: print a message to show whether syntax is correct
)
(define-derived-mode my-config-mode nil "my-config"
(setq-local font-lock-defaults '(my-config-font-lock-keywords))
;; (add-hook 'after-save-hook #'my-check-syntax)
)
(add-to-list 'auto-mode-alist '("\\.myconfigure\\'" . my-config-mode))
Use the LOCAL argument to add-hook and remove-hook.

define keybinding for dired to run a command ("open ") on the file under the cursor

I want to stop using finder in OSX and use dired instead. OSX has a command, "open ", to open any file in the default app from the cli. (Example: "open $the_file"). This works fine with C-! followed by me entering "open " but I'd like to do both those thing with a key combination, say C-0.
How can I create a function that does the equivalent of "C-!" -> "open " in dired when I press C-0 with the cursor over a filepath?
(EDIT: I found a solution, macros!)
I reset lawlist-filename to nil at the end of each example so that it can be used in a variety of other functions, including, but not limited to, dired-read-file-name: dired-read-file-name: pop-up dired mode to read-file-name
EDIT:  Rewritten with the help of #phils -- uses let bindings, instead of global variables.
(eval-after-load "dired" '(progn
;; open anything externally with OSX default app -- Wide-Open-Throttle!
(define-key dired-mode-map (kbd "z") (lambda () (interactive)
(let ((lawlist-filename (dired-get-file-for-visit)))
(start-process "default-app" nil "open" lawlist-filename))))
;; open *.pdf file externally with the OSX default *.pdf viewer.
(define-key dired-mode-map (kbd "<SPC>") (lambda () (interactive)
(let ((lawlist-filename (dired-get-file-for-visit)))
(if (equal (file-name-extension lawlist-filename) "pdf")
(start-process "default-pdf-app" nil "open" lawlist-filename)))))
;; open *.pdf file externally using Skim.
(define-key dired-mode-map (kbd "v") (lambda () (interactive)
(let* (
(lawlist-filename (dired-get-file-for-visit))
(skim "/Applications/Skim.app/Contents/MacOS/Skim") )
(if (equal (file-name-extension lawlist-filename) "pdf")
(start-process "pdf-with-skim" nil "open" "-a" skim lawlist-filename)))))
))
This works, but it seems inelegant:
http://www.emacswiki.org/emacs/KeyboardMacros
I used a macro:
(fset 'my_macro
[?! ?o ?p ?e ?n ? return])

Elisp/texi2dvi: How to call texi2dvi from Emacs?

I try to write a function based on the code from: Latex, Emacs: automatically open *TeX Help* buffer on error and close it after correction of the error?
I would like to replace latexmk by texi2dvi, but TeX-master-file does not contain the file ending .tex (which seems to be required for texi2dvi). I found out that one can add .tex by using TeX-master-file t. However, I can't make it work (I'm not an elisp programmer). Here is what I tried:
;; texi2dvi
(defun run-texi2dvi ()
(interactive)
(let ((TeX-save-query nil)
(TeX-process-asynchronous nil)
(master-file (expand-file-name (TeX-master-file t)))); append `.tex`
(TeX-save-document "")
(TeX-run-TeX "texi2dvi"
(TeX-command-expand "PDFLATEX='pdflatex -synctex=1' texi2dvi -p %s" 'TeX-master-file)
master-file)
(if (plist-get TeX-error-report-switches (intern master-file))
(TeX-next-error t)
(progn
(demolish-tex-help)
(minibuffer-message "texi2dvi: done.")))))
No clue if there's a better way to do it, but this version should work. Basically, TeX-command-expand was given the function TeX-master-file as a symbol which was called internally, and there it was called without the I-want-the-extension argument. The replacing lambda forces that.
(defun run-texi2dvi ()
(interactive)
(let ((TeX-save-query nil)
(TeX-process-asynchronous nil)
(master-file (expand-file-name (TeX-master-file t)))); append `.tex`
(TeX-save-document "")
(TeX-run-TeX "texi2dvi"
(TeX-command-expand
"PDFLATEX='pdflatex -synctex=1' texi2dvi -p %s"
(lambda (ext-ignored nondir)
(TeX-master-file t nondir)))
master-file)
(if (plist-get TeX-error-report-switches (intern master-file))
(TeX-next-error t)
(progn
(demolish-tex-help)
(minibuffer-message "texi2dvi: done.")))))
See here for a more detailed description of the issue and a simple workaround: https://tex.stackexchange.com/questions/67244/how-to-set-up-texi2dvi-with-synctex-and-error-handling/67384#67384

Resources