How to always show inline images? - image

I'm trying to work with inline images (e.g. for plotting data by gnuplot), and have the problem: images always inserted as links by default. I need to do some keypresses to "force" emacs to show actual image inline, instead of just file link.
E.g. I start with gnuplot code:
#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC
When I press C-c C-c on this code block, it runs, and shows me results as link to image file:
#+RESULTS:
[[file:plot.png]]
If I press C-c C-x C-v (org-toggle-inline-images) twice -- link does replaced with inline image
If I run M-x org-redisplay-inline-images -- again, link does replaced with image
If I run (org-display-inline-images t t) -- again, image is shown
and so on (those options were taken from Emacs org-display-inline-images and Inline images in org-mode questions)
But I don't want to press anything special: I want images to be displayed inline by default. I've found and tried following variables:
(setq org-startup-with-inline-images t) in .emacs config
#+STARTUP: inlineimages header
(setq org-display-inline-images t)
But neither got me the behavior I want. I'm puzzled -- do I want something so unnatural?
P.S. Im' using GNU Emacs v26.1 on MacOS X, org mode v9.1.9-65, if it matters
P.P.S. Although it seems like a bug in my emacs/orgmode version, and I'm yet to report it, but meanwhile I've found following trick: (add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append) (thanks to ob-ipython authors) -- it fixes issue for me right now. Maybe will be useful for somebody else

I can reproduce the problem with:
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 # /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)
Reproduction:
Start Emacs 26.3 with emacs -Q.
M-x load-library RET org RET
Add Gnuplot to org-babel-load-languages via M-x customize-option.
Load gnuplot.el
Open the Org file with the following content and press C-c C-c on the source block.
#+STARTUP: inlineimages
Some text.
#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC
I have a similar solution as you suggested in your question, but a bit more differentiated.
Re-displaying images in large Org documents can take some time. So I do it only if the source block has the results-parameter graphics:
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images after executing source blocks with graphics results."
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images)))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)

#Tobias is the best answer. I have tweaked the #Tobias code to further optimize by bounding the re-display to the current subtree and setting the REFRESH parameter to t to redisplay only if necessary.
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images in subtree if cursor in source block with :result graphics."
(when (org-in-src-block-p)
(let (beg end)
(save-excursion
(org-mark-subtree)
(setq beg (point))
(setq end (mark)))
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images nil t beg end)))))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)

Related

Trouble running Common Music in Emacs

I am a 4th year bachelor student composition who recently came across the GitHub repositories of Prof. Orm Finnendahl. I was wondering if anyone could help me setup Common Music 2.12.0 with Emacs on Windows 7, 64 bit. I’m embarrassed to say I have been trying on and off now for a couple of months and just can’t figure it out! I’ve read CM’s documentation, but it simply doesn’t seem to work.
I’ve tried following the instructions on the older website dedicated to CM2 (prior to CM3). Specifically these two links: "Installing Common Music", and "Running Common Music in Emacs". I did get the Emacs part right, I think, but Bash gives an error trying to load anything.
(I put the cm-master without the -master, and other repositories in C:/Users/XI-USER/lispmusic)
My Emacs is, I believe properly setup, as follows in the init file (the last lines are from the "Running Common Music in Emacs" website):
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(load (expand-file-name "~/quicklisp/slime-helper.el"))
(setq inferior-lisp-program "sbcl")
(global-linum-mode t)
(add-hook 'prog-mode-hook 'highlight-numbers-mode)
(add-hook 'emacs-lisp-mode-hook 'highlight-quoted-mode)
(add-hook 'prog-mode-hook #'rainbow-delimiters-mode)
(add-hook 'emacs-lisp-mode-hook 'highlight-defined-mode)
(use-package kaolin-themes
:config
(load-theme 'kaolin-galaxy t))
;; If t, enable italic style in comments.
(setq kaolin-themes-italic-comments t)
;; When t, will display colored hl-line style instead monochrome.
(setq kaolin-themes-hl-line-colored t)
;; Enable distinct background for fringe and line numbers.
(setq kaolin-themes-distinct-fringe t)
;; Enable distinct colors for company popup scrollbar.
(setq kaolin-themes-distinct-company-scrollbar t)
;; Show git-gutter indicators as solid lines
(setq kaolin-themes-git-gutter-solid t)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages (quote (use-package)))
'(tool-bar-mode nil))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "Victor Mono" :foundry "outline" :slant normal :weight bold :height 120 :width normal)))))
(add-to-list 'load-path "C:/Users/XI-USER/.slime/")
(add-to-list 'load-path "C:/Users/XI-USER/lispmusic/cm/")
(load "etc/emacs/cm.el")
(enable-cm-commands)
I have both Git Bash and cygwin64 installed (I believe Bash is 64 bit too, should it matter)
When I load cm/bin/cm.sh via Emacs shell, I get this error message in Bash:
debugger invoked on a SB-INT:SIMPLE-FILE-ERROR in thread
#<THREAD "main thread" RUNNING {10027900C3}>:
Couldn't load "/c/Users/XI-USER/lispmusic/cm/bin/../src/cm.lisp": file does
not exist.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE] Ignore runtime option --eval "(progn (load \"/c/Users/XI-USER/lispmusic/cm/bin/../src/cm.lisp\" :verbose nil) (cm))".
1: [ABORT ] Skip rest of --eval and --load options.
2: Skip to toplevel READ/EVAL/PRINT loop.
3: [EXIT ] Exit SBCL (calling #'EXIT, killing the process).
(LOAD "/c/Users/XI-USER/lispmusic/cm/bin/../src/cm.lisp" :VERBOSE NIL :PRINT NIL :IF-DOES-NOT-EXIST T :EXTERNAL-FORMAT :DEFAULT)
0]
And nothing via cygwin64 (black DOS screen starts up and closes immediately)
I get these type of errors trying to load CM in Emacs:
M-x cm -> "Wrong type argument: stringp, nil"
and CL-USER> (in-package :cm) -> "The name "CM" does not designate any package.
[Condition of type SB-KERNEL:SIMPLE-PACKAGE-ERROR]"
Interestingly, when (and only when) I start up slime, and then type C-c C-d c the CM find dictionary does work!

Debugging Maxima CAS Lisp code on Emacs

Is it possible to debug Maxima CAS Lisp code in Emacs?
It's a pain to use so many print statements all the time.
I've used two approaches over the years.
Run slime using the Maxima core file. See this email for how to do it
http://article.gmane.org/gmane.comp.mathematics.maxima.general/36029
Run Maxima but add code in the initialisation file to create a swank server then connect to that with slime-connect.
http://article.gmane.org/gmane.comp.mathematics.maxima.general/44533
Someone (Leo Butler, maybe?) on the list then suggested a neater approach than what's in that email. Unfortunately, my searching-fu has failed me and I can't find the conversation so I'll just paste what's in my ~/.maxima/swank.lisp nowadays:
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *swank-asd*
(car (directory #P"~/.emacs.d/elpa/slime*/swank.asd")))
(when *swank-asd*
(load *swank-asd*)
(require :swank)))
(when (find-package :swank)
(swank:create-server :port 56789 :dont-close t)
;; Hack to make "q" not kill Maxima outright. Only applies from console
(in-package :maxima)
(defvar *real-continue-function* (symbol-function 'continue))
(setf (symbol-function 'continue)
(lambda (&rest args)
(let ((swank::*sldb-quit-restart* 'maxima::macsyma-quit))
(apply *real-continue-function* args))))
(format t "Swank loaded successfully"))
It starts by trying to load up swank from my Emacs directory (I install slime using Elpa). On success, or if swank was loaded anyway for some reason, it creates a server and then does the nifty "make the q key not really annoying" hack described in the second email.

Emacs: cmdproxy.exe has encountered an issue and needs to close

I opened up emacs today and I got an error when I started typing into the #include:
This only happens when I start typing. At first I thought it had something to do with my ~/.emacs file so I opened it up and commented certain things. Eventually I found that when I comment the following line the problem goes away:
(ac-config-default)
I'm using yasnippet and auto-complete packages in my lisp file for my editor. The problem seems to be the above line when using auto-complete This is the full script of my ~/.emacs up to that point:
(require 'cc-mode)
(load (expand-file-name "~/quicklisp/slime-helper.el"))
;; Replace "sbcl" with the path to your implementation
(setq inferior-lisp-program "sbcl")
(setq-default c-basic-offset 4 c-default-style "linux")
(setq-default tab-width 4 indent-tabs-mode t)
(define-key c-mode-base-map (kbd "RET") 'newline-and-indent)
; start package.el with emacs
(require 'package)
; add MELPA to repository list
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
; initialize package.el
(package-initialize)
; start auto-complete with emacs
(require 'auto-complete)
; do default config for auto config
(require 'auto-complete-config)
; THIS LINE IS THE PROBLEM:
(ac-config-default)
Is there something wrong I've done here? Something I may have forgotten to do?
First try running emacs as admin and see if the problem goes away, I've had trouble with permissions issues in windows before similar to this.
The error is almost certainly caused by the gtags autocomplete source for C.
If you are not using gtags, you can likely fix the problem by removing the source from autocomplete.
(add-hook 'c-mode-common-hook
(lambda () (remove-from-list 'ac-sources 'ac-source-gtags)) t t)
Not a perfect solution, but a possible one.

ANSI Coloring in Compilation Mode

Have anyone added support for ansi-color in compilation-mode Emacs? If so what property/attribute does the color-writing program have to check for in order to make sure its active terminal supports ANSI-escape coloring.
There's already a function for applying color to comint buffers. You simply need to enable it on compilation buffers:
(require 'ansi-color)
(defun colorize-compilation-buffer ()
(toggle-read-only)
(ansi-color-apply-on-region compilation-filter-start (point))
(toggle-read-only))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
Color writing programs should check the TERM environment variable and the terminfo database to check if the terminal supports color. In practice, a lot of programs ignore this and rely on a user setting. Emacs will set the compilation terminal type to dumb by default but this can be overriden by setting the compilation-environment variable.
Update: Note that in Emacs 24.5 the two calls to (toggle-read-only) in the code above are not needed.
My optimized solution which don't pollute M-x grep (only for M-x compile):
(ignore-errors
(require 'ansi-color)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
As of emacs 28.1, this is now a built in !
(require 'ansi-color)
(add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)
Or with use-package
(use-package ansi-color
:hook (compilation-filter . ansi-color-compilation-filter))
Riffing on #gavenkoa's solution:
(when (require 'ansi-color nil t)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
This will not block errors but will still not raise an error if ansi-color is unavailable. Personally, I find the wildcard catch semantics of ignore-error distasteful.
Riffing on #stribb's solution, which riffs on #gavenkoa's solution, this is how to set it up with the awesome use-package:
(use-package ansi-color
:config
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
:hook (compilation-filter . my-colorize-compilation-buffer))
As of 2020, the most modern way appears to be the xterm-color Emacs package.
See my answer on the duplicate question for details.

How can I get mouse selection to work in emacs and iTerm2 on Mac?

after turning on xterm-mouse-mode, any click on the in the screen returns "mouse-1 is undefined". What should I be defining "mouse-1" as? Is there some reason my mouse clicks are returning this event, and not others its suspecting?
For Emacs in iTerm 2, I've found that the following bit in my ~/.emacs file works very well, providing the ability to insert the character at an arbitrary location, mark a region, and use the scroll wheel:
;; Enable mouse support
(unless window-system
(require 'mouse)
(xterm-mouse-mode t)
(global-set-key [mouse-4] (lambda ()
(interactive)
(scroll-down 1)))
(global-set-key [mouse-5] (lambda ()
(interactive)
(scroll-up 1)))
(defun track-mouse (e))
(setq mouse-sel-mode t)
)
I put this in my .emacs:
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
and that seems to do the trick, and now a mouse click in a split changes focus to the split.
Note: I am using iterm2, and I found the info here: http://groups.google.com/group/iterm2-discuss/browse_thread/thread/8e1f2ee7db26c07d/17ac15e69c554998?show_docid=17ac15e69c554998&pli=1
Mac OS X's Terminal.app does not support mouse reporting. However MouseTerm is a SIMBL plugin that provides it with this feature. http://bitheap.org/mouseterm/
Install MouseTerm and put the following in your Emacs config file:
; enable mouse reporting for terminal emulators
(unless window-system
(xterm-mouse-mode 1)
(global-set-key [mouse-4] (lambda ()
(interactive)
(scroll-down 1)))
(global-set-key [mouse-5] (lambda ()
(interactive)
(scroll-up 1))))
I suspect that installing the emacs-goodies-el will provide the appropriate bindings.

Resources