Can't pass variable to secure-hash - elisp

Why does secure-hash fail when I use a variable on the second call? Emacs 27.1, 27.2 and 26.3
(setq mytext "test")
(message (secure-hash 'sha256' mytext))
(message (secure-hash 'sha256' "test"))

You have an extra quote ' before mytext that prevents evaluation of the mytext variable.
The following works just fine:
(let ((mytext "test"))
(secure-hash 'sha256 mytext))
==> "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
(secure-hash 'sha256 "test")
==> "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
PS. If you are new to Lisp and this was not just a typo, you will greatly benefit from reading "An Introduction to Programming in Emacs
Lisp".
The best way to do it is in Emacs: C-h i m intro TAB RET.

Related

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

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)

Emacs in batch mode: function on buffer does not give the expected result

The terminal command
emacs --batch --eval "
(dolist (file command-line-args-left)
(with-current-buffer (find-file-noselect file) (progn (princ
(current-buffer)) (count-lines-page)) ) )
"
filename.org
gives
"Page has 0 lines (0 + 0) filename.org"
as answer. In Emacs, the correct answer is given (as the file has lines). I am struggling to understand why. The same applies to other functions like org-lint. In addition, the ordering is not as defined by progn: "filename.org" is printed before the answer. I wonder if that is connected.
(Emacs 28.1)

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)))

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