rotate document in emacs doc-view-mode - emacs

I have a document displayed in a doc-view buffer. However, the document is rotated 90 degrees left. Can I rotate a doc in emacs doc-view?

I also had this requirement. Sadly doc-view doesn't provide this functionality.
Also the image code used by emacs can't rotate images. So I created a function which utilizes ImageMagick to transform the png files stored in the cache dir and redisplay the current page:
(defun doc-view-rotate-current-page ()
"Rotate the current page by 90 degrees. Requires ImageMagick installation"
(interactive)
(when (eq major-mode 'doc-view-mode)
;; we are assuming current doc-view internals about cache-names
(let ((file-name (expand-file-name (format "page-%d.png" (doc-view-current-page)) (doc-view--current-cache-dir))))
;; assume imagemagick is installed and rotate file in-place and redisplay buffer
(call-process-shell-command "convert" nil nil nil "-rotate" "90" (concat "\"" file-name "\"") (concat "\"" file-name "\""))
(clear-image-cache)
(doc-view-goto-page (doc-view-current-page))))))

Related

Org LaTeX preview is fuzzy on retina displays

I have been using Emacs 24.4 for all my math/scientific notes. org-latex-preview is fantastic for this! But recently, I upgraded to a macbook pro with retina display, and I now see that all my equations in org-mode are... fuzzy. Is there a setting I can change to up-res these?
Here is a screenshot:
Thanks!
A couple of years back, I decided to fix this and wrote a patch to add dvisvgm as a render option for latex previews. While this worked great, I never submitted it (no time or knowledge on how to officially patch org).
Today, I was thrilled to discover that org-mode v9.0.6, now has this feature!
To activate, first check that you have dvisvgm on your system. Then update org-mode and add the following line to your init.el file:
(setq org-latex-create-formula-image-program 'dvisvgm)
And presto!
I found a solution that works a little more generally for all inline images. First make sure any generated images are created with a scale factor of 2. For example for LaTeX code blocks and inline LaTeX snippets this works by
(plist-put org-format-latex-options :scale 2)
Then you make org scale all inlined images back.
For LaTeX code blocks we can advise org--create-inline-image like so:
(defun my/image-scale-advice (image)
(let* ((factor (image-property image :scale))
(new-factor (if factor
(/ factor 2.0)
0.5)))
(image--set-property image :scale new-factor)
image))
(advice-add 'org--create-inline-image :filter-return #'my/image-scale-advice)
This divides any already existing scaling-factor by 2 or sets a scaling factor of 0.5 if none is present.
For inline LaTeX snippets we can advise org--make-preview-overlay like so:
(defun my/overlay-scale-advice (beg end image &optional imagetype)
(mapc (lambda (ov) (if (equal (overlay-get ov 'org-overlay-type) 'org-latex-overlay)
(overlay-put ov
'display
(list 'image :type (or (intern imagetype) 'png) :file image :ascent 'center :scale 0.5))))
(overlays-at beg)))
(advice-add 'org--make-preview-overlay :after #'my/overlay-scale-advice)
This should result in much crispier inline images on Retina displays.
By default orgmode latex preview do not support retina, so on mac with retina screen, latex preview will be fuzzy.
However, we can hack org.el to achieve the function. Just follow steps below:
get the proper version of emacs
To instead use the Yamamoto Mitsuharu version of Emacs 25.1 (with more mac-specific features):
brew tap railwaycat/emacsmacport
brew install emacs-mac
and finally link it to your Applications folder:
brew linkapps emacs-mac
this version emacs will support retina image display.
change org-format-latex-options
change scale from 1.0 to 2.0, to generate 2x size image.
delete org.elc
add function to org.el
(defun galaxy-compose-image-filename-2x(image-file-name)
(concat (file-name-directory image-file-name) (file-name-base image-file-name) "#2x." (file-name-extension image-file-name)))
and eval the function.
modify function org-format-latex
change fragment:
(unless (file-exists-p movefile)
(org-create-formula-image value movefile options forbuffer processing-type)
to
(unless (file-exists-p movefile)
(org-create-formula-image value movefile options forbuffer processing-type)
(setq filename-2x (galaxy-compose-image-filename-2x movefile))
(rename-file movefile filename-2x)
(call-process-shell-command "convert" nil nil nil nil (concat "\"" filename-2x "\" -scale \"50%\" -quality \"100%\"" ) (concat "\"" movefile "\"" )))
and eval the function.
Now, you can preview latex with 2x size image for mac retina screen.
I have tried the
emacs-mac-port
If I create 2 files foo.png foo#2x.png on the same dir ,this will work.

How to configure Emacs to use variable fonts to display the previsualisation of LaTeX formulas?

I would like to use Emacs 24.2.1 in orgmode 7.9.3 on a data projector in class.
I can pre-visualize latex formulas using org-preview-latex-fragment (C-c C-x C-l), and
I can increase the font used to display the text using text-scale-increase (C-+).
The problem is that the math fonts are not increasing along the text fonts.
Is there a way to configure Emacs so that the math fonts are variable too?
Was this solved in a later version of orgmode (I was planning to wait for the end of the term to upgrade to version 8+, being afraid of having to invest a lot of time in updating my configuration).
A poor man's solution that works with advices and hooks is shown below.
Paste the code into your .emacs configuration file and re-start emacs. Afterwards, the equation images should be scaled at along with the text if you press C-+/-/0. They are just previews and their The bad thing is that the resolution is not enlarged. To get equation images with a good resolution hit C-c C-x C-l to regenerate the images. Note, that this sets the :scale property of the customization variable org-format-latex-options. The customization of this variable will be lost for the current emacs session.
You need a gnu-emacs with built-in imagemagick. Should be standard by now.
(defvar text-scale-mode-hook nil
"Hook run at end of command `text-scale-mode'.")
(defadvice text-scale-mode (after text-scale-mode-hooks nil activate)
"Run `text-scale-mode-hook' at end of command `text-scale-mode'."
(if (functionp text-scale-mode-hook)
(funcall text-scale-mode-hook)
(loop for hook in text-scale-mode-hook do
(if (eq hook 't)
(run-hooks (default-value text-scale-mode-hook))
(run-hooks hook)))))
(defun org-text-scale-eye ()
"Scale equation images according to text-scale-mode-amount."
(when (boundp 'text-scale-mode-amount)
(let ((relwidth (* (expt text-scale-mode-step text-scale-mode-amount))))
(loop for ol in (overlays-in (point-min) (point-max)) do
(when (eq (overlay-get ol 'org-overlay-type) 'org-latex-overlay)
(unless (overlay-get ol 'org-image-original-width)
(overlay-put ol 'org-image-original-width (car (image-size (overlay-get ol 'display) t))))
(let ((ol-disp-plist (cdr (overlay-get ol 'display))))
(setq ol-disp-plist (plist-put ol-disp-plist :type 'imagemagick))
(setq ol-disp-plist (plist-put ol-disp-plist :width (round (* relwidth (overlay-get ol 'org-image-original-width)))))
(overlay-put ol 'display (append '(image) ol-disp-plist))
))))
(force-window-update)
))
(add-hook 'org-mode-hook '(lambda () (add-hook 'text-scale-mode-hook 'org-text-scale-eye)))
(defadvice org-format-latex (before set-scale activate)
"Set :scale in `org-format-latex-options' to the scaling factor resulting from `text-scale-mode' and clear cache."
(let ((relwidth (expt text-scale-mode-step text-scale-mode-amount)))
(unless (= (plist-get org-format-latex-options :scale) relwidth)
(plist-put org-format-latex-options :scale relwidth))))
Actually, I had a look at the code in org.el:
The font height is regarded in the function org-create-formula-image-with-dvipng and the resolution option -D is set. Maybe, there goes something wrong. Furthermore, pityingly, in org-format-latex the images are cached and not reproduced after font size changes.
If you want to increase the size of the latex-formulas for one session in general with good quality you can set the :scale value of the customization option "Org Format Latex Options" in the group "org-latex" to a higher value.
Edit 2013-10-10 (marked as italic throughout the answer except the code, naturally): The :scale property of the option org-format-latex-options is now set automagically. Therefore, the above hack should be quite usable (such as the latex version mentioned below).
The same problem as you describe in the original-post exists for latex-preview. But, for that case there is a better solution:
(require 'face-remap)
(defadvice preview-inherited-face-attribute (after preview-inherit-local-face nil activate)
"Scale preview images with respect to buffer-local face"
(when (and text-scale-mode (eq attribute :height))
(setq ad-return-value (* (expt text-scale-mode-step text-scale-mode-amount) ad-return-value))))
After C-+/-/0 type C-c C-p C-d to re-generate all formulas of the document. Afterwards, the formulas have just the right size and the right resolution.
If switching to latex documents with preview is an option for you I would recommend it. I use org only for notes and time-tables with easy links.
For more descriptive documents I use latex documents with auctex/preview. But, I actually compile them only very seldom to a final pdf document. The preview is good enough. See the following Figure.

Inline PDF images in org-mode

In emacs+org-mode, when viewing an org-mode buffer, you can inline
linked images with the org-toggle-inline-images command. This
includes various formats out of the box, but apparently PDF images
aren't included yet.
Given that emacs is perfectly capable of rendering PDF files, is it
possible to make org-mode inline PDF files like it does with images
(png,jpeg,etc)?
Some background: PDF images are more convenient for me for several
reasons, the biggest being that they scale well and work well with
latex, from small papers to large posters.
Let me finish this question.
Firstly, Org-mode does not support any pdf inline display function with itself. However, it is possible to modify org-display-inline-images to achieve what you want. First you need to refer to this answer: Configuring emacs for showing fixed width inline images, which inspired me a lot. Then I slightly modified the function, making it support pdf, bmp display in org-mode. My function is on below.
(setq image-file-name-extensions
(quote
("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm" "xpm" "pbm" "pgm" "ppm" "pnm" "svg" "pdf" "bmp")))
(setq org-image-actual-width 600)
(setq org-imagemagick-display-command "convert -density 600 \"%s\" -thumbnail \"%sx%s>\" \"%s\"")
(defun org-display-inline-images (&optional include-linked refresh beg end)
"Display inline images.
Normally only links without a description part are inlined, because this
is how it will work for export. When INCLUDE-LINKED is set, also links
with a description part will be inlined. This
can be nice for a quick
look at those images, but it does not reflect what exported files will look
like.
When REFRESH is set, refresh existing images between BEG and END.
This will create new image displays only if necessary.
BEG and END default to the buffer boundaries."
(interactive "P")
(unless refresh
(org-remove-inline-images)
(if (fboundp 'clear-image-cache) (clear-image-cache)))
(save-excursion
(save-restriction
(widen)
(setq beg (or beg (point-min)) end (or end (point-max)))
(goto-char beg)
(let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
(substring (org-image-file-name-regexp) 0 -2)
"\\)\\]" (if include-linked "" "\\]")))
old file ov img)
(while (re-search-forward re end t)
(setq old (get-char-property-and-overlay (match-beginning 1)
'org-image-overlay)
file (expand-file-name
(concat (or (match-string 3) "") (match-string 4))))
(when (file-exists-p file)
(let ((file-thumb (format "%s%s_thumb.png" (file-name-directory file) (file-name-base file))))
(if (file-exists-p file-thumb)
(let ((thumb-time (nth 5 (file-attributes file-thumb 'string)))
(file-time (nth 5 (file-attributes file 'string))))
(if (time-less-p thumb-time file-time)
(shell-command (format org-imagemagick-display-command
file org-image-actual-width org-image-actual-width file-thumb) nil nil)))
(shell-command (format org-imagemagick-display-command
file org-image-actual-width org-image-actual-width file-thumb) nil nil))
(if (and (car-safe old) refresh)
(image-refresh (overlay-get (cdr old) 'display))
(setq img (save-match-data (create-image file-thumb)))
(when img
(setq ov (make-overlay (match-beginning 0) (match-end 0)))
(overlay-put ov 'display img)
(overlay-put ov 'face 'default)
(overlay-put ov 'org-image-overlay t)
(overlay-put ov 'modification-hooks
(list 'org-display-inline-remove-overlay))
(push ov org-inline-image-overlays))))))))))
The function uses convert file.pdf -thumbnail "400x400>" file_thumb.png to generate a file_thumb named thumbnail in your folder to substitute overlay of pdf, and force org-mode to display pdf with file_thumb without any modification to the org file.
Moreover, because i use babel to generate image with python. It always need me to update the _thumb file, so I add a if condition to say if this thumb file existed or not, and if the pdf file changed i need to change thumb file on the same time... and so on!
Hope it can help you.
Short answer: no support for PDF inline images.
The function org-image-file-name-regexp has the image file extensions hardcoded (and does not include PDF). This function is used by org-display-inline-images, which in turn calls create-image.
I've tried adding PDF to org-image-file-name-regexp, and deleting PDF from imagemagik-types-inhibit with no luck.
You may try to dig further, or request the feature to org-mode mailing list.
I ran into the same problem and after poking around a bit, got it to work by adding the following to my .emacs file:
(add-to-list 'image-type-file-name-regexps '("\\.pdf\\'" . imagemagick))
(add-to-list 'image-file-name-extensions "pdf")
(setq imagemagick-types-inhibit (remove 'PDF imagemagick-types-inhibit))
(setq org-image-actual-width 600)
Not sure yet, if there are any issues for other modes by these changes.
My pdfs in org mode are created by python src code blocks and the above works, but the images don't get updated when I change something in the python mode and I need to run org-display-inline-images by hand.
This assumes that you have an emacs with imagemagick support, something similar should also work for using ghostscript (with perhaps some more edits in org.el).

Emacs org-mode: textual reference to a file:line

I am using org-mode in Emacs to document my development activities. One of the tasks which I must continuously do by hand is to describe areas of code. Emacs has a very nice Bookmark List: create a bookmark with CTRL-x r m, list them with CTRL-x r l. This is very useful, but is not quite what I need.
Org-mode has the concept of link, and the command org-store-link will record a link to the current position in any file, which can be pasted to the org-file. The problem with this is two-fold:
It is stored as an org-link, and the linked position is not directly visible (just the description).
It is stored in the format file/search, which is not what I want.
I need to have the bookmark in textual form, so that I can copy paste it into org-mode, end edit it if needed, with a simple format like this:
absolute-file-path:line
And this must be obtained from the current point position. The workflow would be as simple as:
Go to the position which I want to record
Call a function: position-to-kill-ring (I would bind this to a keyboard shortcut)
Go to the org-mode buffer.
Yank the position.
Edit if needed (sometimes I need to change absolute paths by relative paths, since my code is in a different location in different machines)
Unfortunately my lisp is non-existent, so I do not know how to do this. Is there a simple solution to my problem?
(defun position-to-kill-ring ()
"Copy to the kill ring a string in the format \"file-name:line-number\"
for the current buffer's file name, and the line number at point."
(interactive)
(kill-new
(format "%s:%d" (buffer-file-name) (save-restriction
(widen) (line-number-at-pos)))))
You want to use the org-create-file-search-functions and org-execute-file-search-functions hooks.
For example, if you need the search you describe for text-mode files, use this:
(add-hook 'org-create-file-search-functions
'(lambda ()
(when (eq major-mode 'text-mode)
(number-to-string (line-number-at-pos)))))
(add-hook 'org-execute-file-search-functions
'(lambda (search-string)
(when (eq major-mode 'text-mode)
(goto-line (string-to-number search-string)))))
Then M-x org-store-link RET will do the right thing (store a line number as the search string) and C-c C-o (i.e. M-x org-open-at-point RET) will open the file and go to this line number.
You can of course check for other modes and/or conditions.
An elisp beginner myself I though of it as a good exercise et voila:
Edit: Rewrote it using the format methode, but I still think not storing it to the kill-ring is less intrusive in my workflow (don't know about you). Also I have added the capability to add column position.
(defvar current-file-reference "" "Global variable to store the current file reference")
(defun store-file-line-and-col ()
"Stores the current file, line and column point is at in a string in format \"file-name:line-number-column-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d:%d" (buffer-file-name) (line-number-at-pos) (current-column))))
(defun store-file-and-line ()
"Stores the current file and line oint is at in a string in format \"file-name:line-number\". Insert the string using \"insert-file-reference\"."
(interactive)
(setq current-file-reference (format "%s:%d" (buffer-file-name) (line-number-at-pos))))
(defun insert-file-reference ()
"Inserts the value stored for current-file-reference at point."
(interactive)
(if (string= "" current-file-reference)
(message "No current file/line/column set!")
(insert current-file-reference)))
Not tested extensively but working for me. Just hit store-file-and-line or store-file-line-and-col to store current location and insert-file-reference to insert the stored value at point.
BTW, if you want something better than FILE:LINE, you can try to use add-log-current-defun (in add-log.el) which should return the name of the current function.
;; Insert a org link to the function in the next window
(defun insert-org-link-to-func ()
(interactive)
(insert (with-current-buffer (window-buffer (next-window))
(org-make-link-string
(concat "file:" (buffer-file-name)
"::" (number-to-string (line-number-at-pos)))
(which-function)
))))
This func generates link with the function name as the description.
Open two windows, one is the org file and the other is src code.
Then M-x insert-org-link-to-func RET

Emacs: TODO indicator on left fringe has a strange side-effect - deleting characters

I just read Emacs :TODO indicator at left side, and tried it out. It seems intriguing. The little indicator triangles appear, but I'm getting a weird side effect: the text itself is being altered. Characters are being deleted.
Before:
After:
The mode-line does indicate that the buffer has been altered after running annotate-todo.
What explains this?
(I'm using emacs 22.2.1 on Windows)
Ahhh... I see the error of my ways earlier. Here's a new version.
(defun annotate-todo ()
"put fringe marker on TODO: lines in the curent buffer"
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "TODO:" nil t)
(let ((overlay (make-overlay (- (point) 5) (point))))
(overlay-put overlay 'before-string (propertize (format "A")
'display '(left-fringe right-triangle)))))))
The first solution used a the 'display text property, which changes how the specified text is displayed, in this case it was replaced by the triangle in the left fringe. What I needed to do was to use a 'before-string overlay instead. Which doesn't change the string being displayed.
Another advantage, the cut/paste of the code annotated by this does not carry the markup.
I've updated the code in the original question to reflect this change as well.