photogal.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;; PHOTOGAL ;;;;;;;;;;;;;;;;;;;;;`;
  3. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; v1.0 ;;;;;;
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;,;;;;;;;;;*;;;
  5. ;; ` ;;
  6. ;; author: jordyn , - * ;;
  7. ;; authored: spokane valley, summer '22 . ` ;;
  8. ;; * ^ ~ ';
  9. ;; PHOTO * , ' . ` * , ;;
  10. ;; , Grouper ' ` . * - . ;;
  11. ;; . And , ^ ' . ' . ` ` ' ;;
  12. ;; ` Labeler ' , * ' * ;;
  13. ;; , . , ` ' . ` ;;
  14. ;; ' - ' , ;;
  15. ;; ;;
  16. ;; ;;
  17. ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;
  18. ;;;; -- ---- -- - DATA - -- ---- -- ;;;;
  19. (defvar *photogal/operating-photo-dir* nil)
  20. (defvar *photogal/all-photos* nil)
  21. (defvar *photogal/operating-table* nil)
  22. (defvar *photogal/--resize-photo* nil)
  23. (defvar *photogal/photos-origin-directory* nil)
  24. (defcustom photogal-default-directory "/Users/jwd/bench/photos/"
  25. "This is where photogal will look for photos.")
  26. (defcustom photogal/tags
  27. '(("a" . "art")
  28. ("c" . "cityscape")
  29. ("f" . "family")
  30. ("g" . "good")
  31. ("h" . "screenshot")
  32. ("l" . "politics")
  33. ("m" . "meme")
  34. ("o" . "computer")
  35. ("p" . "portrait")
  36. ("r" . "reaction-photo")
  37. ("t" . "photography")
  38. ("s" . "selfie"))
  39. "Tags and key-command to associate to photos.")
  40. (defvar *photogal/commands*
  41. '(("RET" . "Next")
  42. ("C-p" . "Prev")
  43. ("C-a" . "Add tag")
  44. ("C-d" . "Delete tag")
  45. ("C-f" . "Show filename")
  46. ("C-g" . "Refresh buffer")
  47. ("C-r" . "Resize photo")
  48. ("C-c" . "Commit all")
  49. ("C-n" . "Name the file")
  50. ("C-o" . "Add a dir")))
  51. (defun photogal-get-tags-for-file (photo-filepath)
  52. "what tags does this file have?"
  53. (photogal--get-tags (photogal--lookup-photo photo-filepath)))
  54. (defun photogal-set-tags-for-file (photo-filepath tags)
  55. "make this file have these tags"
  56. (photogal--set-tags (photogal--lookup-photo photo-filepath) tags))
  57. (defun photogal-get-folders-for-file (photo-filepath)
  58. "what folders does this file have?"
  59. (photogal--get-folders (photogal--lookup-photo photo-filepath)))
  60. (defun photogal-set-folders-for-file (photo-filepath folders)
  61. "make this file have these folders"
  62. (photogal--set-folders (photogal--lookup-photo photo-filepath) folders))
  63. (defun photogal-get-name-for-file (photo-filepath)
  64. "does this file have a user-given name?"
  65. (photogal--get-name (photogal--lookup-photo photo-filepath)))
  66. (defun photogal-set-name-for-file (photo-filepath name)
  67. "give this file a Proper name. (embedded in final filename) (optional)"
  68. (photogal--set-name (photogal--lookup-photo photo-filepath) name))
  69. (defun photogal-mark-photo-for-copying (photo-filepath)
  70. "toggle on to copy this file. Warning: marks file for committing."
  71. (plist-put (photogal--lookup-photo photo-filepath)
  72. 'copy-to-dir t))
  73. (defun photogal-unmark-photo-for-copying (photo-filepath)
  74. "toggle on to NOT copy this file. Warning: will not commit file."
  75. (plist-put (photogal--lookup-photo photo-filepath)
  76. 'copy-to-dir nil))
  77. (defun photogal-photo-valid-for-committing? (photo-filepath)
  78. (let ((all-fields-for-photo
  79. (mapcar (lambda (field) (plist-get (photogal--lookup-photo photo-filepath)
  80. field))
  81. '(tags name))))
  82. (seq-some (lambda (field) (not (eq nil field)))
  83. all-fields-for-photo)))
  84. (defun photogal-file-marked-for-copying? (photo-filepath)
  85. (plist-get (photogal--lookup-photo photo-filepath)
  86. 'copy-to-dir ))
  87. ;; // internal \\ ;;
  88. (defun photogal--lookup-photo (photo-filepath)
  89. (seq-find (lambda (photo)
  90. (string= (photogal--get-filepath photo) photo-filepath))
  91. *photogal/operating-table*))
  92. (defun photogal--get-filepath (photo)
  93. (plist-get photo 'filepath))
  94. (defun photogal--get-tags (photo)
  95. "What are all the tags for this file?"
  96. (plist-get photo 'tags))
  97. (defun photogal--set-tags (photo tags)
  98. (plist-put photo 'tags
  99. tags))
  100. (defun photogal--get-folders (photo)
  101. "What are all the folders for this file?"
  102. (plist-get photo 'folders))
  103. (defun photogal--set-folders (photo folders)
  104. (plist-put photo 'folders
  105. folders))
  106. (defun photogal--get-name (photo)
  107. (plist-get photo 'name))
  108. (defun photogal--set-name (photo name)
  109. (plist-put photo 'name
  110. name))
  111. ;; \\ internal // ;;
  112. (defun photogal-advance-photo ()
  113. "Move forward by one photo."
  114. (setq *photogal/all-photos*
  115. (append (cdr *photogal/all-photos*) (list (car *photogal/all-photos*)))))
  116. (defun photogal-rewind-photo ()
  117. "Reverse by one photo."
  118. (setq *photogal/all-photos*
  119. (append (last *photogal/all-photos*) (butlast *photogal/all-photos*))))
  120. ;;;; -- ---- -- - THE APP - -- ---- -- ;;;;
  121. (defun photogal (photo-dir)
  122. (interactive
  123. (list (read-directory-name
  124. "where are ur photos? " photogal-default-directory)))
  125. (setq *photogal/photos-origin-directory* (directory-file-name photo-dir))
  126. (setq *photogal/operating-photo-dir* (concat *photogal/photos-origin-directory* "-photogal"))
  127. (setq *photogal/all-photos* (photogal-all-photos *photogal/photos-origin-directory*))
  128. (photogal-init (photogal-current-file))
  129. (photogal-init-operating-table)
  130. (photogal-generate-tag-commands))
  131. (defun photogal-restart ()
  132. (interactive)
  133. (setq *photogal/all-photos* (photogal-all-photos *photogal/photos-origin-directory*))
  134. (photogal-init-operating-table)
  135. (photogal-init (photogal-current-file)))
  136. ;;;; -- ---- -- - INITIALIZATION - -- ---- -- ;;;;
  137. (defun photogal-make-photo (filepath)
  138. `(filepath ,filepath
  139. tags ,nil
  140. name ,nil
  141. folders ,(list *photogal/operating-photo-dir*)
  142. copy-to-dir ,nil))
  143. (defun photogal-init-operating-table ()
  144. (setq *photogal/operating-table*
  145. (mapcar (lambda (photo)
  146. (photogal-make-photo photo))
  147. *photogal/all-photos*)))
  148. (defun photogal-generate-tag-commands ()
  149. "Generate and activate M-x (photogal-toggle-tag-TAG) to tag curent photo,
  150. for all tags defined -- one function per tag."
  151. (mapcar (lambda (tag)
  152. (let ((tag-key (car tag))
  153. (tag-name (cdr tag)))
  154. (eval `(photogal-generate-tagger ,(intern tag-name)))
  155. (define-key photogal-mode-map (kbd tag-key)
  156. (intern (format "photogal-toggle-tag-%s" tag-name)))))
  157. photogal/tags))
  158. ;;;; -- ---- -- - TAG - -- ---- -- ;;;;
  159. (defun photogal-add-tag (new-tag new-tag-code)
  160. "Add a user-generated tag to the tag library."
  161. (interactive "sNew tag: \nsTag code (length 1): ")
  162. (let ((tag-code-too-long (> (length new-tag-code) 1))
  163. (tag-code-in-use (photogal-tag-code-in-use new-tag-code)))
  164. (if (or tag-code-too-long tag-code-in-use)
  165. (progn (message "tag code must be a single character and can't be already in use")
  166. (call-interactively 'photogal-add-tag))
  167. (photogal--add-tag new-tag new-tag-code)
  168. (photogal-refresh-buffer))))
  169. (defun photogal-delete-tag (tag-code)
  170. "Remove a tag from the library."
  171. (interactive "sDelete tag: ")
  172. (customize-save-variable
  173. 'photogal/tags
  174. (seq-remove (lambda (tag) (string= (car tag) tag-code)) photogal/tags))
  175. (photogal-refresh-buffer))
  176. (defun photogal--add-tag (new-tag new-tag-code)
  177. "Modify the defcustom var to the new collection of tags."
  178. (let ((escaped-str-tag (string-replace " " "-" new-tag)))
  179. (customize-save-variable
  180. 'photogal/tags
  181. (cons (cons new-tag-code escaped-str-tag) photogal/tags)))
  182. (photogal-generate-tag-commands))
  183. (defun photogal-tag-code-in-use (tag-code)
  184. (seq-contains-p
  185. photogal/tags tag-code
  186. (lambda (tag test) (string= (car tag) test))))
  187. ;;;; -- ---- -- - TAGGING FILES - -- ---- -- ;;;;
  188. (defun photogal-for-file-toggle-tag (file tag)
  189. "If a file has the tag, remove it. If it doesn't have it, add it."
  190. (if (photogal-file-has-tag? file tag)
  191. (photogal-rm-tag-from-file file tag)
  192. (photogal-add-tag-to-file file tag)))
  193. (defun photogal-add-tag-to-file (file tag)
  194. "Append new tag for a file."
  195. (let ((tags (photogal-get-tags-for-file file)))
  196. (photogal-set-tags-for-file file
  197. (seq-sort #'string< (seq-uniq (cons tag tags))))))
  198. (defun photogal-rm-tag-from-file (file tag)
  199. "Dissociate tag from file."
  200. (defun tags-without-tag (tags tag)
  201. (seq-sort
  202. #'string<
  203. (seq-uniq
  204. (seq-remove
  205. (lambda (tg) (string= tg tag)) tags))))
  206. (let ((tags (photogal-get-tags-for-file file)))
  207. (photogal-set-tags-for-file file
  208. (tags-without-tag tags tag))))
  209. (defun photogal-file-has-tag? (file tag)
  210. "Does this file have this tag?"
  211. (let ((tags (photogal-get-tags-for-file file)))
  212. (seq-contains-p tags tag)))
  213. ;;;; -- ---- -- - DEST DIRS - -- ---- -- ;;;;
  214. (defun photogal-add-folder-for-file (file folder)
  215. "Append new folder for a file."
  216. (let ((folders (photogal-get-folders-for-file file)))
  217. (photogal-set-folders-for-file file
  218. (seq-sort #'string< (seq-uniq (cons folder folders))))))
  219. (defun photogal-give-a-folder (name)
  220. (interactive ;"sWhat folder do u wannan put this in ")
  221. (list (read-directory-name
  222. "What folder do u wannan put this in " photogal-default-directory)))
  223. (let ((folder-name (directory-file-name name)))
  224. (photogal-add-folder-for-file (photogal-current-file) folder-name)
  225. (photogal-mark-photo-for-copying (photogal-current-file))
  226. (photogal-refresh-buffer)))
  227. ;;;; -- ---- -- - FILE NAME - -- ---- -- ;;;;
  228. (defun photogal-name-the-file (name)
  229. (interactive "sWhat do u want to name this file? ")
  230. (photogal-set-name-for-file
  231. (photogal-current-file)
  232. (string-replace " " "-" name))
  233. (photogal-mark-photo-for-copying (photogal-current-file))
  234. (photogal-refresh-buffer))
  235. ;;;; -- ---- -- - FILE OPS - -- ---- -- ;;;;
  236. (defun photogal-all-photos (directory)
  237. "Give me a list of all the photos in my operating directory."
  238. (directory-files directory
  239. t directory-files-no-dot-files-regexp))
  240. (defun photogal-current-file ()
  241. "What is the file currently being operated on?"
  242. (car *photogal/all-photos*))
  243. ;;;; -- ---- -- - U I - -- ---- -- ;;;;
  244. (defun photogal-init (photo-file-path &optional show-filepath)
  245. "Set everything up in the buffer."
  246. (let ((buf (get-buffer-create "photogal")))
  247. (with-current-buffer buf
  248. (photogal-mode)
  249. (erase-buffer)
  250. (photogal-index-tracker)
  251. (if (photogal-file-marked-for-copying? photo-file-path)
  252. (progn
  253. (insert "\t\t\t\t will commit?: ")
  254. (photogal--insert-print-color "✓" "SeaGreen3"))
  255. (progn
  256. (insert "\t\t\t\t will commit?: ")
  257. (photogal--insert-print-color "✗" "red")))
  258. (insert "\n")
  259. (insert " ")
  260. (insert-image
  261. (if *photogal/--resize-photo*
  262. (create-image photo-file-path 'imagemagick nil
  263. :width (- (window-pixel-width) 75))
  264. (create-image photo-file-path 'imagemagick nil
  265. :height (/ (window-pixel-height) 2))))
  266. (insert "\n\nCurrent tags: ")
  267. (insert (format "%s" (photogal-get-tags-for-file photo-file-path)))
  268. (let ((padding "\n"))
  269. (if (photogal-get-name-for-file photo-file-path)
  270. (insert (format "\nName: %s" (photogal-get-name-for-file photo-file-path)))
  271. (setq padding (concat padding "\n")))
  272. (if (photogal-get-folders-for-file photo-file-path)
  273. (photogal--insert-print-color
  274. (format "\ndest dir: %s"
  275. (photogal-get-folders-for-file photo-file-path))
  276. "light gray")
  277. (setq padding (concat padding "\n")))
  278. (insert padding))
  279. (insert "\n")
  280. (photogal--insert-print-color "Tag:\n" "red")
  281. (photogal-insert-tags-to-buffer photogal/tags)
  282. (photogal--insert-print-color "\n\nCommands:\n" "red")
  283. (photogal-insert-commands-to-buffer
  284. *photogal/commands*)
  285. (when show-filepath
  286. (insert "\n\n")
  287. (insert (photogal-current-file))))
  288. (switch-to-buffer buf)))
  289. (defun photogal-next-file ()
  290. (interactive)
  291. (photogal-advance-photo)
  292. (photogal-refresh-buffer))
  293. (defun photogal-prev-file ()
  294. (interactive)
  295. (photogal-rewind-photo)
  296. (photogal-refresh-buffer))
  297. (defun photogal-refresh-buffer (&optional show-filepath)
  298. "Refresh buffer."
  299. (interactive)
  300. ;; (message "refreshing buffer") ;; useful to know when screen re-draws
  301. (progn ; useful stuff to run every page draw
  302. (if (not (photogal-photo-valid-for-committing? (photogal-current-file)))
  303. (photogal-unmark-photo-for-copying (photogal-current-file))))
  304. (photogal-init (photogal-current-file) show-filepath)
  305. (photogal-generate-tag-commands)
  306. (beginning-of-buffer))
  307. (defun photogal-resize-photo ()
  308. (interactive)
  309. (setq *photogal/--resize-photo* (not *photogal/--resize-photo*))
  310. (photogal-refresh-buffer))
  311. (defun photogal-show-filepath ()
  312. (interactive)
  313. (photogal-refresh-buffer t))
  314. (defun photogal-index-tracker ()
  315. ;; this is a little expensive, running photogal-all-photos
  316. ;; on every paint, but i'd like to have the file count
  317. ;; be very accurate.
  318. (let ((current-index
  319. (+ 1 (seq-position
  320. (photogal-all-photos *photogal/photos-origin-directory*)
  321. (photogal-current-file))))
  322. (total-photos
  323. (length (photogal-all-photos *photogal/photos-origin-directory*))))
  324. (insert " ur lookin at photo ")
  325. (photogal--insert-print-color current-index "red")
  326. (insert " of ")
  327. (photogal--insert-print-color total-photos "red")))
  328. ;;;; -- ---- -- - LO-LEVEL DISPLAY - -- ---- -- ;;;;
  329. ;; this stuff paints the words on the screen, changing ;;
  330. ;; color, etc, pprinting stuff at a pretty granular and ;;
  331. ;; tediously technical level. ;;
  332. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  333. (defun photogal-insert-tags-to-buffer (tags)
  334. "Pretty print the tags with their toggle key."
  335. (mapcar (lambda (tag)
  336. (let* ((key-command (car tag))
  337. (tag-name (cdr tag))
  338. (activated (photogal-file-has-tag? (photogal-current-file) tag-name)))
  339. (photogal--pprint-key-command key-command tag-name 16 activated)))
  340. (seq-sort (lambda (t1 t2) (string< (car t1) (car t2))) tags)))
  341. (defun photogal-insert-commands-to-buffer (commands)
  342. "Pretty print the commands with their invoke key."
  343. (mapcar (lambda (command)
  344. (let ((key-command (car command))
  345. (command-name (cdr command)))
  346. (photogal--pprint-key-command key-command command-name 24)))
  347. commands))
  348. (defun photogal--pprint-key-command (key-to-type name-of-command padding &optional activated)
  349. "Make the low-level insertions to the buffer to render a key-command."
  350. (let ((length-of-unit (+ (length key-to-type) (length name-of-command) 3)))
  351. (when (> (+ (+ (current-column) length-of-unit)
  352. 10)
  353. (window-width))
  354. (insert "\n"))
  355. (insert "[")
  356. (if activated
  357. (photogal--insert-print-color key-to-type "SeaGreen3")
  358. (photogal--insert-print-color key-to-type "dark gray"))
  359. (insert "] ")
  360. (photogal--insert-print-color name-of-command "blue" (- padding (length key-to-type)))
  361. (insert " ")))
  362. (defun photogal--insert-print-color (string-to-insert-to-buffer color &optional padding)
  363. "Insert some text in this color."
  364. (let ((beg (point))
  365. (padding
  366. (if padding
  367. (format "%s" padding)
  368. "0")))
  369. (insert (format (concat "%-" padding "s") string-to-insert-to-buffer))
  370. (put-text-property beg (point) 'font-lock-face `(:foreground ,color))))
  371. ;;;; -- ---- -- - META SHIT - -- ---- -- ;;;;
  372. ;; this is the coolest damn thing here. ;;
  373. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  374. (defmacro photogal-generate-tagger (name)
  375. "Generate function to toggle a tag which is itself on the current file.
  376. One of these is needed per tag. For instance if you want to create the tag
  377. 'cool', you can run and evaluate (photogal-generate-tagger cool) to create a new
  378. function `photogal-toggle-tag-cool` that will toggle the tag 'cool' for
  379. the current file."
  380. (let ((my-funcname (intern (format "photogal-toggle-tag-%s" name))))
  381. `(defun ,my-funcname ()
  382. (interactive)
  383. (photogal-for-file-toggle-tag (photogal-current-file) ,(format "%s" name))
  384. (photogal-mark-photo-for-copying (photogal-current-file))
  385. (photogal-refresh-buffer))))
  386. ;;;; -- ---- -- -MOVING FILES AROUND- -- ---- -- ;;;;
  387. (defun photogal-files--get-extension (filepath)
  388. (file-name-extension filepath))
  389. (defun photogal-files--generate-unique-identifier (filepath)
  390. "Not GUARANTEED unique, but probably unique enough for my purposes."
  391. (seq-take (md5 (concat (current-time-string) filepath))
  392. 6))
  393. (defun photogal-files--new-file-name-for-photo (filepath tags name)
  394. (cons
  395. filepath
  396. (let (( new-name (concat
  397. (photogal-files--generate-unique-identifier filepath)
  398. "-"
  399. (format-time-string "%M%H,%d%m%y")
  400. "-"
  401. name
  402. "-_"
  403. (string-join tags "_")
  404. "_")))
  405. (if (file-name-extension filepath)
  406. (file-name-with-extension new-name (file-name-extension filepath))
  407. new-name))))
  408. (defun photogal-files--new-filenames-for-photos ()
  409. (mapcar
  410. (lambda (photo)
  411. (let ((filepath (photogal--get-filepath photo))
  412. (tags (photogal--get-tags photo))
  413. (name (photogal--get-name photo)))
  414. (photogal-files--new-file-name-for-photo filepath tags name)))
  415. *photogal/operating-table*))
  416. (defun photogal-compile-and-commit ()
  417. (interactive)
  418. (if (y-or-n-p (format "Are u sure? "))
  419. (photogal-heavy-move-files-to-directory)
  420. (message "whoops")))
  421. (defun photogal-heavy-move-files-to-directory ()
  422. ;; THIS DOES A LOTTA SHIT!!!
  423. (defun rename-file-to-folders (file-rename)
  424. (let ((origin (car file-rename))
  425. (new-name (cdr file-rename)))
  426. (when (photogal-photo-valid-for-committing? origin)
  427. (let ((dest-dirs (photogal-get-folders-for-file origin)))
  428. (mapcar (lambda (directory)
  429. (make-directory directory 'parents)
  430. (let ((new-file-name (expand-file-name new-name directory)))
  431. (message (format "renaming %s to %s" origin new-file-name))
  432. (copy-file origin new-file-name)))
  433. dest-dirs)
  434. (delete-file origin)))))
  435. (let* ((new-names (photogal-files--new-filenames-for-photos)))
  436. (mapcar
  437. #'rename-file-to-folders
  438. new-names)
  439. (photogal-restart)))
  440. ;;;; -- ---- -- - KEY BINDINGS - -- ---- -- ;;;;
  441. (defvar photogal-mode-map nil "Keymap for `photogal-mode`")
  442. (defvar key-commands
  443. '(("G" . photogal-refresh-buffer)
  444. ("RET" . photogal-next-file)
  445. ("<right>" . photogal-next-file)
  446. ("SPC" . photogal-next-file)
  447. ("C-p" . photogal-prev-file)
  448. ("<left>" . photogal-prev-file)
  449. ("C-a" . photogal-add-tag)
  450. ("C-d" . photogal-delete-tag)
  451. ("C-f" . photogal-show-filepath)
  452. ("C-r" . photogal-resize-photo)
  453. ("C-c" . photogal-compile-and-commit)
  454. ("C-n" . photogal-name-the-file)
  455. ("C-o" . photogal-give-a-folder)))
  456. (progn
  457. (setq photogal-mode-map (make-sparse-keymap))
  458. (map-do (lambda (key command)
  459. (eval `(define-key photogal-mode-map (kbd ,key) ',command)))
  460. key-commands))
  461. (define-derived-mode photogal-mode text-mode "photogal"
  462. "Major mode for grouping and labeling images.")