Content AND Presentation

(Note to English-speaking readers: the links entitled Komentarze na tej stronie lead to comment pages.)

2016-06-20 Easy Javascript logging

I’ve been doing some JavaScript coding recently. Before you run off screaming, let me tell you that JavaScript is not that bad. Yes, it has C-like, unlispy syntax, but at its heart it is quite a nice language. Today, though, I didn’t want to write about JS in general, but about one small detail. While debugging JavaScript code, it is often useful to sprinkle console.log statements in your code. Also, if you want to check the value of some more complicated variable, you need to JSON.stringify it first. Of course, writing repetitive pieces of code is not what an Emacs user would like to do, so I hacked this:

(defun js-insert-console-log ()
  "Insert a console.log statement in the line below.
If region is active, treat it as the variable to log."
  (interactive)
  (let ((variable (if (use-region-p)
		      (buffer-substring-no-properties (region-beginning)
						      (region-end))
		    nil)))
    (move-end-of-line 1)
    (newline-and-indent)
    (if variable
	(save-excursion
	  (insert
	   (format "console.log(\"%s: \" + JSON.stringify(%s, null, '\\t'));"
		   variable
		   variable))
	  (deactivate-mark))
      (insert "console.log();")
      (backward-char 2))))

Note that this is really only a quick hack – it doesn’t use js2-mode’s JS parsing capabilities, for instance (I’d like to understand them some day, but I’d have to learn about EIEIO first). After having used it quite intensely for a few days now, though, I haven’t noticed any problems.

(Of course, it is only a poor substitute for real debugging; this SO answer gives a few possibilities. I’ll look into this in some Free Time™.)

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryJavaScript

Comments on this page

2016-06-13 Displaying pdfs on the right

Some time ago I decided that I’ll give PDF tools a shot.

Wow. Just wow. Never going back to Evince. (Well, almost never – Evince does have one or two options not present in PDF Tools, but I hardly ever use them). If you are an Emacs user on GNU+Linux, I strongly advice you to try PDF Tools out. Especially if you do a lot of LaTeX work in AUCTeX (like I do) and/or if you need to read or create PDF annotations (highlighting and/or notes) – it’s really great. (I did once experience some problems with said annotations, though.) I was a bit afraid about the efficiency – but man, it’s really fast! A great piece of work indeed.

I have a few problems with it, though. The biggest one is that C-M-v (scroll-other-window) won’t switch to next page of the pdf. (Emacs’ doc-view has the same problem, and it seems that it’s not trivial to change, at least not without advice). Another one is that sometimes, when I zoom in a bit and do something, my carefully found place in the pdf is gone, and some other place is shown. But none of these is a deal-breaker.

One thing I found really annoying (though it’s not related to PDF Tools – again, doc-view has the same problem): when I pressed C-c C-v (TeX-view), the pdf was shown in a window below the current one. Since most of my documents are in “portrait” mode, this doesn’t make sense at all – I’d very much prefer if the window with the pdf was shown to the right of the current one.

OTOH, I would not want everything to pop up to the right. I use a laptop with a 14” screen, which is just a tad too small for that to be comfortable. (And I’m totally not accustomed to such a behavior.)

Of course, the solution is known already: display-buffer-alist. However, while we’re at it, there is one more thing to take care of: I don’t want the window containing the pdf to be half the screen wide. It doesn’t really make sense, either. The aspect ratio of my screen is 16:9, which means that a horizontal split divides the main window into two almost square halves. OTOH, I use almost exclusively the A4 paper size, whose aspect ratio is √2:1. (Those strange Americans who use those strange paper sizes might want to adjust for that, although even they are not strange enough to use square paper for most of their documents.) This means that by dividing the window in the middle, I waste a lot of screen real estate.

It turns out that making Emacs split windows in the desired proportion is not a trivial task (see here and here for some pointers). In this case, however, it’s not a big problem: I can find out the correct value of the argument for split-window-right by trial and error, and this is exactly what I did.

All in all, let the code speak for itself. It is not very elegant in that I just copy-pasted the split-window-sensibly function and made the changes I needed, but I don’t see how I could achieve my goal in a more elegant way with, say, advice, so who cares. If you want different settings, just play around with the two variables introduced below.

Finally, notice that my previous version of the regex for buffers displayed on the right, which was just "\\.pdf$", does not work if there are two pdfs with the same name, but different directories displayed at the same time, since by default Emacs appends a string of the form <directory> to the end of the buffer name then. Pretty rare, I’d argue, but it actually happened to me, and it was not obvious at first what was going on!

(defvar pdf-minimal-width 72
  "Minimal width of a window displaying a pdf.
If an integer, number of columns.  If a float, fraction of the
original window.")

(defvar pdf-split-width-threshold 120
  "Minimum width a window should have to split it horizontally
for displaying a pdf in the right.")

(defun pdf-split-window-sensibly (&optional window)
  "A version of `split-window-sensibly' for pdfs.
It prefers splitting horizontally, and takes `pdf-minimal-width'
into account."
  (let ((window (or window (selected-window)))
	(width (- (if (integerp pdf-minimal-width)
		      pdf-minimal-width
		    (round (* pdf-minimal-width (window-width window)))))))
    (or (and (window-splittable-p window t)
	     ;; Split window horizontally.
	     (with-selected-window window
	       (split-window-right width)))
	(and (window-splittable-p window)
	     ;; Split window vertically.
	     (with-selected-window window
	       (split-window-below)))
	(and (eq window (frame-root-window (window-frame window)))
	     (not (window-minibuffer-p window))
	     ;; If WINDOW is the only window on its frame and is not the
	     ;; minibuffer window, try to split it vertically disregarding
	     ;; the value of `split-height-threshold'.
	     (let ((split-height-threshold 0))
	       (when (window-splittable-p window)
		 (with-selected-window window
		   (split-window-below))))))))

(defun display-buffer-pop-up-window-pdf-split-horizontally (buffer alist)
  "Call `display-buffer-pop-up-window', using `pdf-split-window-sensibly'
when needed."
  (let ((split-height-threshold nil)
	(split-width-threshold pdf-split-width-threshold)
	(split-window-preferred-function #'pdf-split-window-sensibly))
    (display-buffer-pop-up-window buffer alist)))

(add-to-list 'display-buffer-alist '("\\.pdf\\(<[^>]+>\\)?$" . (display-buffer-pop-up-window-pdf-split-horizontally)))

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX

Comments on this page

2016-06-06 Org clocking and resolving idle time

This post is basically a reminder for myself, though some of you might find it useful.

I use the Org-mode clocking feature a lot. (Especially now that I have it synchronized with Beeminder.) While I’m not a hardcore QS fanatic, I do see their point that collecting objective, measurable data about oneself can give very valuable insights. (For instance, I learned that if you have an 8-hour-per-day job, and you spend 6 hours actually working, then you are insanely efficient – time efficiency of 50% percent is normal according to my experience.

Sometimes it happens, though, that I forget to clock out. In such a case, org-clock-idle-time (set to 5 minutes) comes to my rescue. After 5 minutes of doing nothing on the computer (which might mean that I went away and forgot about the running clock – but it could as well mean that I’m studying a scientific paper!) Emacs displays a large menu, with a lot of options – and it’s all too easy to get lost. Reading about the choices every time is wasteful, but OTOH it does not happen often enough that I can easily memorize them. Hence this post.

It turns out that I really only need three of these options:

  • i (ignore the question, continue with the clock),
  • S (that is, shift-s – probably the most useful – indicates that all idle time should be subtracted from the clock).
  • K RET (that is, shift-k RET; basically almost the same as clock out now),

Read the relevant page in the Org manual for details – but most probably you will need i and S 95% of the time (and with K, you are most probably covered in 99%).

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode

Comments on this page

More...

(Więcej means More in Polish; click it to see older entries.)

CategoryEnglish, CategoryBlog