How visit a file in a new frame - elisp

When programmin in Elisp, how can I visit a new file in a new frame and switch focus on that?
-- I mean after doing some stuff in a function I create a file "test.xml", At the end of my function I want to open this xml file in a new frame and show it to the user.
I have tried something like the following but when executed shows a blank buffer:
(defun transform-file-on-save ()
...
(new-file-name (concat (buffer-file-name) ".xml"))
...
(write-region (point-min) (point-max) new-file-name nil 'quietly)
(switch-to-buffer-other-window new-file-name)

Use (find-file-other-frame FILENAME &optional WILDCARDS) instead of switch-to-buffer-other-window new-file-name)

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)

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

Archiving all the done states

I want to archive all the DONE todo states in a file, The below code extends the function specified in the link (please look at the comment in the link to see why the answer provided does not work)
How to archive all the DONE tasks using a single command
as below
(defun my-org-archive-subtree ()
(if (string-match "^\*.* DONE" (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
(setq org-map-continue-from (point-at-bol))
(setq org-map-continue-from nil))
(org-archive-subtree))
(defun my-org-archive-done-tasks ()
(interactive)
(org-map-entries 'my-org-archive-subtree "/DONE" (list "/home/myhome/org/office.org")))
which seems to get in to infinite loop for the below contents in the file
office.org while execting the s2 heading
* s1
** DONE ss1
* s2
** NEXT ss2
** sss3
Why does it goes in to infinite loop and how to solve the problem.
Update:-
This was due to the bug in the org-scan-tags function which was called by
org-map-entries fn. The bug has been reported and fixed. To view the discussions
on the mailing list click here

Resources