Skip to content

Instantly share code, notes, and snippets.

@bjonnh
Created September 12, 2020 16:07
Show Gist options
  • Save bjonnh/e28322518c24dc4f0085a2c4b6091ae4 to your computer and use it in GitHub Desktop.
Save bjonnh/e28322518c24dc4f0085a2c4b6091ae4 to your computer and use it in GitHub Desktop.
(defvar org-screenshot-process nil
"Currently running screenshot process")
;; Because cl is deprecated
(defun plusp (arg)
(if (> arg 0) true nil )
)
(defun org-screenshot-take (&optional delay)
"Take a screenshot and insert link to it at point, if image
display is already on (see \\[org-toggle-inline-images])
screenshot will be displayed as an image
Screen area for the screenshot is selected with the mouse, left
click on a window screenshots that window, while left click and
drag selects a region. Pressing any key cancels the screen shot
With `C-u' universal argument waits one second after target is
selected before taking the screenshot. With double `C-u' wait two
seconds.
With triple `C-u' wait 3 seconds, and also rings the bell when
screenshot is done, any more `C-u' after that increases delay by
2 seconds
"
(interactive "P")
;; probably easier way to count number of C-u C-u out there
(setq delay
(cond ((null delay) 0)
((integerp delay) delay)
((and (consp delay)
(integerp (car delay))
(plusp (car delay)))
(let ((num 1)
(limit (car delay))
(cnt 0))
(while (< num limit)
(setq num (* num 4)
cnt (+ cnt (if (< cnt 3) 1 2))))
cnt))
(t (error "Invalid delay"))))
(when (and org-screenshot-process
(member (process-status org-screenshot-process)
'(run stop)))
(error "scrot process is still running"))
(let* ((name (org-screenshot-generate-file-name (org-screenshot-image-directory)))
(file (format "%s%s" (org-screenshot-image-directory)
name))
(path (expand-file-name file)))
(when (get-buffer "*scrot*")
(with-current-buffer (get-buffer "*scrot*")
(erase-buffer)))
(setq org-screenshot-process
(or
(apply 'start-process
(append
(list "scrot" "*scrot*" "gnome-screenshot" "-a" "-f" path)
(when (plusp delay)
(list "-d" (format "%d" delay)))))
(error "Unable to start scrot process")))
(when org-screenshot-process
(if (plusp delay)
(message "Click on a window, or select a rectangle (delay is %d sec)..."
delay)
(message "Click on a window, or select a rectangle..."))
(set-process-sentinel
org-screenshot-process
`(lambda (process event)
(org-screenshot-process-done
process event ,file ,(current-buffer) ,delay ',last-input-event))))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment