image-dired.el 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  1. ;;; image-dired.el --- use dired to browse and manipulate your images -*- lexical-binding: t -*-
  2. ;;
  3. ;; Copyright (C) 2005-2017 Free Software Foundation, Inc.
  4. ;;
  5. ;; Version: 0.4.11
  6. ;; Keywords: multimedia
  7. ;; Author: Mathias Dahl <mathias.rem0veth1s.dahl@gmail.com>
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;
  21. ;; BACKGROUND
  22. ;; ==========
  23. ;;
  24. ;; I needed a program to browse, organize and tag my pictures. I got
  25. ;; tired of the old gallery program I used as it did not allow
  26. ;; multi-file operations easily. Also, it put things out of my
  27. ;; control. Image viewing programs I tested did not allow multi-file
  28. ;; operations or did not do what I wanted it to.
  29. ;;
  30. ;; So, I got the idea to use the wonderful functionality of Emacs and
  31. ;; `dired' to do it. It would allow me to do almost anything I wanted,
  32. ;; which is basically just to browse all my pictures in an easy way,
  33. ;; letting me manipulate and tag them in various ways. `dired' already
  34. ;; provide all the file handling and navigation facilities; I only
  35. ;; needed to add some functions to display the images.
  36. ;;
  37. ;; I briefly tried out thumbs.el, and although it seemed more
  38. ;; powerful than this package, it did not work the way I wanted to. It
  39. ;; was too slow to created thumbnails of all files in a directory (I
  40. ;; currently keep all my 2000+ images in the same directory) and
  41. ;; browsing the thumbnail buffer was slow too. image-dired.el will not
  42. ;; create thumbnails until they are needed and the browsing is done
  43. ;; quickly and easily in dired. I copied a great deal of ideas and
  44. ;; code from there though... :)
  45. ;;
  46. ;; `image-dired' stores the thumbnail files in `image-dired-dir'
  47. ;; using the file name format ORIGNAME.thumb.ORIGEXT. For example
  48. ;; ~/.emacs.d/image-dired/myimage01.thumb.jpg. The "database" is for
  49. ;; now just a plain text file with the following format:
  50. ;;
  51. ;; file-name-non-directory;comment:comment-text;tag1;tag2;tag3;...;tagN
  52. ;;
  53. ;;
  54. ;; PREREQUISITES
  55. ;; =============
  56. ;;
  57. ;; * The ImageMagick package. Currently, `convert' and `mogrify' are
  58. ;; used. Find it here: http://www.imagemagick.org.
  59. ;;
  60. ;; * For non-lossy rotation of JPEG images, the JpegTRAN program is
  61. ;; needed.
  62. ;;
  63. ;; * For `image-dired-get-exif-data' and `image-dired-write-exif-data' to work,
  64. ;; the command line tool `exiftool' is needed. It can be found here:
  65. ;; http://www.sno.phy.queensu.ca/~phil/exiftool/. These two functions
  66. ;; are, among other things, used for writing comments to image files
  67. ;; using `image-dired-thumbnail-set-image-description' and to create
  68. ;; "unique" file names using `image-dired-get-exif-file-name' (used by
  69. ;; `image-dired-copy-with-exif-file-name').
  70. ;;
  71. ;;
  72. ;; USAGE
  73. ;; =====
  74. ;;
  75. ;; This information has been moved to the manual. Type `C-h r' to open
  76. ;; the Emacs manual and go to the node Thumbnails by typing `g
  77. ;; Image-Dired RET'.
  78. ;;
  79. ;; Quickstart: M-x image-dired RET DIRNAME RET
  80. ;;
  81. ;; where DIRNAME is a directory containing image files.
  82. ;;
  83. ;; LIMITATIONS
  84. ;; ===========
  85. ;;
  86. ;; * Supports all image formats that Emacs and convert supports, but
  87. ;; the thumbnails are hard-coded to JPEG format.
  88. ;;
  89. ;; * WARNING: The "database" format used might be changed so keep a
  90. ;; backup of `image-dired-db-file' when testing new versions.
  91. ;;
  92. ;; * `image-dired-display-image-mode' does not support animation
  93. ;;
  94. ;; TODO
  95. ;; ====
  96. ;;
  97. ;; * Support gallery creation when using per-directory thumbnail
  98. ;; storage.
  99. ;;
  100. ;; * Some sort of auto-rotate function based on rotate info in the
  101. ;; EXIF data.
  102. ;;
  103. ;; * Investigate if it is possible to also write the tags to the image
  104. ;; files.
  105. ;;
  106. ;; * From thumbs.el: Add an option for clean-up/max-size functionality
  107. ;; for thumbnail directory.
  108. ;;
  109. ;; * From thumbs.el: Add setroot function.
  110. ;;
  111. ;; * From thumbs.el: Add image resizing, if useful (image-dired's automatic
  112. ;; "image fit" might be enough)
  113. ;;
  114. ;; * From thumbs.el: Add the "modify" commands (emboss, negate,
  115. ;; monochrome etc).
  116. ;;
  117. ;; * Add `image-dired-display-thumbs-ring' and functions to cycle that. Find
  118. ;; out which is best, saving old batch just before inserting new, or
  119. ;; saving the current batch in the ring when inserting it. Adding it
  120. ;; probably needs rewriting `image-dired-display-thumbs' to be more general.
  121. ;;
  122. ;; * Find some way of toggling on and off really nice keybindings in
  123. ;; dired (for example, using C-n or <down> instead of C-S-n). Richard
  124. ;; suggested that we could keep C-t as prefix for image-dired commands
  125. ;; as it is currently not used in dired. He also suggested that
  126. ;; `dired-next-line' and `dired-previous-line' figure out if
  127. ;; image-dired is enabled in the current buffer and, if it is, call
  128. ;; `image-dired-dired-next-line' and
  129. ;; `image-dired-dired-previous-line', respectively. Update: This is
  130. ;; partly done; some bindings have now been added to dired.
  131. ;;
  132. ;; * Enhanced gallery creation with basic CSS-support and pagination
  133. ;; of tag pages with many pictures.
  134. ;;
  135. ;; * Rewrite `image-dired-modify-mark-on-thumb-original-file' to be
  136. ;; less ugly.
  137. ;;
  138. ;; * In some way keep track of buffers and windows and stuff so that
  139. ;; it works as the user expects.
  140. ;;
  141. ;; * More/better documentation
  142. ;;
  143. ;;
  144. ;;; Code:
  145. (require 'dired)
  146. (require 'format-spec)
  147. (require 'image-mode)
  148. (require 'widget)
  149. (eval-when-compile
  150. (require 'cl-lib)
  151. (require 'wid-edit))
  152. (defgroup image-dired nil
  153. "Use dired to browse your images as thumbnails, and more."
  154. :prefix "image-dired-"
  155. :link '(info-link "(emacs) Image-Dired")
  156. :group 'multimedia)
  157. (defcustom image-dired-dir (locate-user-emacs-file "image-dired/")
  158. "Directory where thumbnail images are stored."
  159. :type 'directory
  160. :group 'image-dired)
  161. (defcustom image-dired-thumbnail-storage 'use-image-dired-dir
  162. "How to store image-dired's thumbnail files.
  163. Image-Dired can store thumbnail files in one of two ways and this is
  164. controlled by this variable. \"Use image-dired dir\" means that the
  165. thumbnails are stored in a central directory. \"Per directory\"
  166. means that each thumbnail is stored in a subdirectory called
  167. \".image-dired\" in the same directory where the image file is.
  168. \"Thumbnail Managing Standard\" means that the thumbnails are
  169. stored and generated according to the Thumbnail Managing Standard
  170. that allows sharing of thumbnails across different programs."
  171. :type '(choice :tag "How to store thumbnail files"
  172. (const :tag "Use image-dired-dir" use-image-dired-dir)
  173. (const :tag "Thumbnail Managing Standard (normal 128x128)" standard)
  174. (const :tag "Thumbnail Managing Standard (large 256x256)" standard-large)
  175. (const :tag "Per-directory" per-directory))
  176. :group 'image-dired)
  177. (defcustom image-dired-db-file
  178. (expand-file-name ".image-dired_db" image-dired-dir)
  179. "Database file where file names and their associated tags are stored."
  180. :type 'file
  181. :group 'image-dired)
  182. (defcustom image-dired-temp-image-file
  183. (expand-file-name ".image-dired_temp" image-dired-dir)
  184. "Name of temporary image file used by various commands."
  185. :type 'file
  186. :group 'image-dired)
  187. (defcustom image-dired-gallery-dir
  188. (expand-file-name ".image-dired_gallery" image-dired-dir)
  189. "Directory to store generated gallery html pages.
  190. This path needs to be \"shared\" to the public so that it can access
  191. the index.html page that image-dired creates."
  192. :type 'directory
  193. :group 'image-dired)
  194. (defcustom image-dired-gallery-image-root-url
  195. "http://your.own.server/image-diredpics"
  196. "URL where the full size images are to be found.
  197. Note that this path has to be configured in your web server. Image-Dired
  198. expects to find pictures in this directory."
  199. :type 'string
  200. :group 'image-dired)
  201. (defcustom image-dired-gallery-thumb-image-root-url
  202. "http://your.own.server/image-diredthumbs"
  203. "URL where the thumbnail images are to be found.
  204. Note that this path has to be configured in your web server. Image-Dired
  205. expects to find pictures in this directory."
  206. :type 'string
  207. :group 'image-dired)
  208. (defcustom image-dired-cmd-create-thumbnail-program
  209. "convert"
  210. "Executable used to create thumbnail.
  211. Used together with `image-dired-cmd-create-thumbnail-options'."
  212. :type 'file
  213. :group 'image-dired)
  214. (defcustom image-dired-cmd-create-thumbnail-options
  215. '("-size" "%wx%h" "%f[0]" "-resize" "%wx%h>" "-strip" "jpeg:%t")
  216. "Options of command used to create thumbnail image.
  217. Used with `image-dired-cmd-create-thumbnail-program'.
  218. Available format specifiers are: %w which is replaced by
  219. `image-dired-thumb-width', %h which is replaced by `image-dired-thumb-height',
  220. %f which is replaced by the file name of the original image and %t
  221. which is replaced by the file name of the thumbnail file."
  222. :version "26.1"
  223. :type '(repeat (string :tag "Argument"))
  224. :group 'image-dired)
  225. (defcustom image-dired-cmd-create-temp-image-program "convert"
  226. "Executable used to create temporary image.
  227. Used together with `image-dired-cmd-create-temp-image-options'."
  228. :type 'file
  229. :group 'image-dired)
  230. (defcustom image-dired-cmd-create-temp-image-options
  231. '("-size" "%wx%h" "%f[0]" "-resize" "%wx%h>" "-strip" "jpeg:%t")
  232. "Options of command used to create temporary image for display window.
  233. Used together with `image-dired-cmd-create-temp-image-program',
  234. Available format specifiers are: %w and %h which are replaced by
  235. the calculated max size for width and height in the image display window,
  236. %f which is replaced by the file name of the original image and %t which
  237. is replaced by the file name of the temporary file."
  238. :version "26.1"
  239. :type '(repeat (string :tag "Argument"))
  240. :group 'image-dired)
  241. (defcustom image-dired-cmd-pngnq-program
  242. (or (executable-find "pngnq")
  243. (executable-find "pngnq-s9"))
  244. "The file name of the `pngnq' program.
  245. It quantizes colors of PNG images down to 256 colors or fewer
  246. using the NeuQuant algorithm."
  247. :version "26.1"
  248. :type '(choice (const :tag "Not Set" nil) file)
  249. :group 'image-dired)
  250. (defcustom image-dired-cmd-pngnq-options
  251. '("-f" "%t")
  252. "Arguments to pass `image-dired-cmd-pngnq-program'.
  253. Available format specifiers are the same as in
  254. `image-dired-cmd-create-thumbnail-options'."
  255. :version "26.1"
  256. :type '(repeat (string :tag "Argument"))
  257. :group 'image-dired)
  258. (defcustom image-dired-cmd-pngcrush-program (executable-find "pngcrush")
  259. "The file name of the `pngcrush' program.
  260. It optimizes the compression of PNG images. Also it adds PNG textual chunks
  261. with the information required by the Thumbnail Managing Standard."
  262. :type '(choice (const :tag "Not Set" nil) file)
  263. :group 'image-dired)
  264. (defcustom image-dired-cmd-pngcrush-options
  265. `("-q"
  266. "-text" "b" "Description" "Thumbnail of file://%f"
  267. "-text" "b" "Software" ,(emacs-version)
  268. ;; "-text b \"Thumb::Image::Height\" \"%oh\" "
  269. ;; "-text b \"Thumb::Image::Mimetype\" \"%mime\" "
  270. ;; "-text b \"Thumb::Image::Width\" \"%ow\" "
  271. "-text" "b" "Thumb::MTime" "%m"
  272. ;; "-text b \"Thumb::Size\" \"%b\" "
  273. "-text" "b" "Thumb::URI" "file://%f"
  274. "%q" "%t")
  275. "Arguments for `image-dired-cmd-pngcrush-program'.
  276. Available format specifiers are the same as in
  277. `image-dired-cmd-create-thumbnail-options', with %q for a
  278. temporary file name (typically generated by pnqnq)"
  279. :version "26.1"
  280. :type '(repeat (string :tag "Argument"))
  281. :group 'image-dired)
  282. (defcustom image-dired-cmd-optipng-program (executable-find "optipng")
  283. "The file name of the `optipng' program."
  284. :type '(choice (const :tag "Not Set" nil) file)
  285. :group 'image-dired)
  286. (defcustom image-dired-cmd-optipng-options '("-o5" "%t")
  287. "Arguments passed to `image-dired-optipng-program'.
  288. Available format specifiers are described in
  289. `image-dired-cmd-create-thumbnail-options'."
  290. :type '(repeat (string :tag "Argument"))
  291. :link '(url-link "man:optipng(1)")
  292. :group 'image-dired)
  293. (defcustom image-dired-cmd-create-standard-thumbnail-options
  294. (append '("-size" "%wx%h" "%f[0]")
  295. (unless (or image-dired-cmd-pngcrush-program
  296. image-dired-cmd-pngnq-program)
  297. (list
  298. "-set" "Thumb::MTime" "%m"
  299. "-set" "Thumb::URI" "file://%f"
  300. "-set" "Description" "Thumbnail of file://%f"
  301. "-set" "Software" (emacs-version)))
  302. '("-thumbnail" "%wx%h>" "png:%t"))
  303. "Options for creating thumbnails according to the Thumbnail Managing Standard.
  304. Available format specifiers are the same as in
  305. `image-dired-cmd-create-thumbnail-options', with %m for file modification time."
  306. :version "26.1"
  307. :type '(repeat (string :tag "Argument"))
  308. :group 'image-dired)
  309. (defcustom image-dired-cmd-rotate-thumbnail-program
  310. "mogrify"
  311. "Executable used to rotate thumbnail.
  312. Used together with `image-dired-cmd-rotate-thumbnail-options'."
  313. :type 'file
  314. :group 'image-dired)
  315. (defcustom image-dired-cmd-rotate-thumbnail-options
  316. '("-rotate" "%d" "%t")
  317. "Arguments of command used to rotate thumbnail image.
  318. Used with `image-dired-cmd-rotate-thumbnail-program'.
  319. Available format specifiers are: %d which is replaced by the
  320. number of (positive) degrees to rotate the image, normally 90 or 270
  321. \(for 90 degrees right and left), %t which is replaced by the file name
  322. of the thumbnail file."
  323. :version "26.1"
  324. :type '(repeat (string :tag "Argument"))
  325. :group 'image-dired)
  326. (defcustom image-dired-cmd-rotate-original-program
  327. "jpegtran"
  328. "Executable used to rotate original image.
  329. Used together with `image-dired-cmd-rotate-original-options'."
  330. :type 'file
  331. :group 'image-dired)
  332. (defcustom image-dired-cmd-rotate-original-options
  333. '("-rotate" "%d" "-copy" "all" "-outfile" "%t" "%o")
  334. "Arguments of command used to rotate original image.
  335. Used with `image-dired-cmd-rotate-original-program'.
  336. Available format specifiers are: %d which is replaced by the
  337. number of (positive) degrees to rotate the image, normally 90 or
  338. 270 \(for 90 degrees right and left), %o which is replaced by the
  339. original image file name and %t which is replaced by
  340. `image-dired-temp-image-file'."
  341. :version "26.1"
  342. :type '(repeat (string :tag "Argument"))
  343. :group 'image-dired)
  344. (defcustom image-dired-temp-rotate-image-file
  345. (expand-file-name ".image-dired_rotate_temp" image-dired-dir)
  346. "Temporary file for rotate operations."
  347. :type 'file
  348. :group 'image-dired)
  349. (defcustom image-dired-rotate-original-ask-before-overwrite t
  350. "Confirm overwrite of original file after rotate operation.
  351. If non-nil, ask user for confirmation before overwriting the
  352. original file with `image-dired-temp-rotate-image-file'."
  353. :type 'boolean
  354. :group 'image-dired)
  355. (defcustom image-dired-cmd-write-exif-data-program
  356. "exiftool"
  357. "Program used to write EXIF data to image.
  358. Used together with `image-dired-cmd-write-exif-data-options'."
  359. :type 'file
  360. :group 'image-dired)
  361. (defcustom image-dired-cmd-write-exif-data-options
  362. '("-%t=%v" "%f")
  363. "Arguments of command used to write EXIF data.
  364. Used with `image-dired-cmd-write-exif-data-program'.
  365. Available format specifiers are: %f which is replaced by
  366. the image file name, %t which is replaced by the tag name and %v
  367. which is replaced by the tag value."
  368. :version "26.1"
  369. :type '(repeat (string :tag "Argument"))
  370. :group 'image-dired)
  371. (defcustom image-dired-cmd-read-exif-data-program
  372. "exiftool"
  373. "Program used to read EXIF data to image.
  374. Used together with `image-dired-cmd-read-exif-data-options'."
  375. :type 'file
  376. :group 'image-dired)
  377. (defcustom image-dired-cmd-read-exif-data-options
  378. '("-s" "-s" "-s" "-%t" "%f")
  379. "Arguments of command used to read EXIF data.
  380. Used with `image-dired-cmd-read-exif-data-program'.
  381. Available format specifiers are: %f which is replaced
  382. by the image file name and %t which is replaced by the tag name."
  383. :version "26.1"
  384. :type '(repeat (string :tag "Argument"))
  385. :group 'image-dired)
  386. (defcustom image-dired-gallery-hidden-tags
  387. (list "private" "hidden" "pending")
  388. "List of \"hidden\" tags.
  389. Used by `image-dired-gallery-generate' to leave out \"hidden\" images."
  390. :type '(repeat string)
  391. :group 'image-dired)
  392. (defcustom image-dired-thumb-size
  393. (cond
  394. ((eq 'standard image-dired-thumbnail-storage) 128)
  395. ((eq 'standard-large image-dired-thumbnail-storage) 256)
  396. (t 100))
  397. "Size of thumbnails, in pixels.
  398. This is the default size for both `image-dired-thumb-width'
  399. and `image-dired-thumb-height'."
  400. :type 'integer
  401. :group 'image-dired)
  402. (defcustom image-dired-thumb-width image-dired-thumb-size
  403. "Width of thumbnails, in pixels."
  404. :type 'integer
  405. :group 'image-dired)
  406. (defcustom image-dired-thumb-height image-dired-thumb-size
  407. "Height of thumbnails, in pixels."
  408. :type 'integer
  409. :group 'image-dired)
  410. (defcustom image-dired-thumb-relief 2
  411. "Size of button-like border around thumbnails."
  412. :type 'integer
  413. :group 'image-dired)
  414. (defcustom image-dired-thumb-margin 2
  415. "Size of the margin around thumbnails.
  416. This is where you see the cursor."
  417. :type 'integer
  418. :group 'image-dired)
  419. (defcustom image-dired-line-up-method 'dynamic
  420. "Default method for line-up of thumbnails in thumbnail buffer.
  421. Used by `image-dired-display-thumbs' and other functions that needs
  422. to line-up thumbnails. Dynamic means to use the available width of
  423. the window containing the thumbnail buffer, Fixed means to use
  424. `image-dired-thumbs-per-row', Interactive is for asking the user,
  425. and No line-up means that no automatic line-up will be done."
  426. :type '(choice :tag "Default line-up method"
  427. (const :tag "Dynamic" dynamic)
  428. (const :tag "Fixed" fixed)
  429. (const :tag "Interactive" interactive)
  430. (const :tag "No line-up" none))
  431. :group 'image-dired)
  432. (defcustom image-dired-thumbs-per-row 3
  433. "Number of thumbnails to display per row in thumb buffer."
  434. :type 'integer
  435. :group 'image-dired)
  436. (defcustom image-dired-display-window-width-correction 1
  437. "Number to be used to correct image display window width.
  438. Change if the default (1) does not work (i.e. if the image does not
  439. completely fit)."
  440. :type 'integer
  441. :group 'image-dired)
  442. (defcustom image-dired-display-window-height-correction 0
  443. "Number to be used to correct image display window height.
  444. Change if the default (0) does not work (i.e. if the image does not
  445. completely fit)."
  446. :type 'integer
  447. :group 'image-dired)
  448. (defcustom image-dired-track-movement t
  449. "The current state of the tracking and mirroring.
  450. For more information, see the documentation for
  451. `image-dired-toggle-movement-tracking'."
  452. :type 'boolean
  453. :group 'image-dired)
  454. (defcustom image-dired-append-when-browsing nil
  455. "Append thumbnails in thumbnail buffer when browsing.
  456. If non-nil, using `image-dired-next-line-and-display' and
  457. `image-dired-previous-line-and-display' will leave a trail of thumbnail
  458. images in the thumbnail buffer. If you enable this and want to clean
  459. the thumbnail buffer because it is filled with too many thumbnails,
  460. just call `image-dired-display-thumb' to display only the image at point.
  461. This value can be toggled using `image-dired-toggle-append-browsing'."
  462. :type 'boolean
  463. :group 'image-dired)
  464. (defcustom image-dired-dired-disp-props t
  465. "If non-nil, display properties for dired file when browsing.
  466. Used by `image-dired-next-line-and-display',
  467. `image-dired-previous-line-and-display' and `image-dired-mark-and-display-next'.
  468. If the database file is large, this can slow down image browsing in
  469. dired and you might want to turn it off."
  470. :type 'boolean
  471. :group 'image-dired)
  472. (defcustom image-dired-display-properties-format "%b: %f (%t): %c"
  473. "Display format for thumbnail properties.
  474. %b is replaced with associated dired buffer name, %f with file name
  475. \(without path) of original image file, %t with the list of tags and %c
  476. with the comment."
  477. :type 'string
  478. :group 'image-dired)
  479. (defcustom image-dired-external-viewer
  480. ;; TODO: Use mailcap, dired-guess-shell-alist-default,
  481. ;; dired-view-command-alist.
  482. (cond ((executable-find "display"))
  483. ((executable-find "xli"))
  484. ((executable-find "qiv") "qiv -t"))
  485. "Name of external viewer.
  486. Including parameters. Used when displaying original image from
  487. `image-dired-thumbnail-mode'."
  488. :type 'string
  489. :group 'image-dired)
  490. (defcustom image-dired-main-image-directory "~/pics/"
  491. "Name of main image directory, if any.
  492. Used by `image-dired-copy-with-exif-file-name'."
  493. :type 'string
  494. :group 'image-dired)
  495. (defcustom image-dired-show-all-from-dir-max-files 50
  496. "Maximum number of files to show using `image-dired-show-all-from-dir'
  497. before warning the user."
  498. :type 'integer
  499. :group 'image-dired)
  500. (defmacro image-dired--with-db-file (&rest body)
  501. "Run BODY in a temp buffer containing `image-dired-db-file'.
  502. Return the last form in BODY."
  503. (declare (indent 0) (debug t))
  504. `(with-temp-buffer
  505. (if (file-exists-p image-dired-db-file)
  506. (insert-file-contents image-dired-db-file))
  507. ,@body))
  508. (defun image-dired-dir ()
  509. "Return the current thumbnails directory (from variable `image-dired-dir').
  510. Create the thumbnails directory if it does not exist."
  511. (let ((image-dired-dir (file-name-as-directory
  512. (expand-file-name image-dired-dir))))
  513. (unless (file-directory-p image-dired-dir)
  514. (make-directory image-dired-dir t)
  515. (message "Creating thumbnails directory"))
  516. image-dired-dir))
  517. (defun image-dired-insert-image (file type relief margin)
  518. "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
  519. (let ((i `(image :type ,type
  520. :file ,file
  521. :relief ,relief
  522. :margin ,margin)))
  523. (insert-image i)))
  524. (defun image-dired-get-thumbnail-image (file)
  525. "Return the image descriptor for a thumbnail of image file FILE."
  526. (unless (string-match (image-file-name-regexp) file)
  527. (error "%s is not a valid image file" file))
  528. (let ((thumb-file (image-dired-thumb-name file)))
  529. (unless (and (file-exists-p thumb-file)
  530. (<= (float-time (nth 5 (file-attributes file)))
  531. (float-time (nth 5 (file-attributes thumb-file)))))
  532. (image-dired-create-thumb file thumb-file))
  533. (create-image thumb-file)
  534. ;; (list 'image :type 'jpeg
  535. ;; :file thumb-file
  536. ;; :relief image-dired-thumb-relief :margin image-dired-thumb-margin)
  537. ))
  538. (defun image-dired-insert-thumbnail (file original-file-name
  539. associated-dired-buffer)
  540. "Insert thumbnail image FILE.
  541. Add text properties ORIGINAL-FILE-NAME and ASSOCIATED-DIRED-BUFFER."
  542. (let (beg end)
  543. (setq beg (point))
  544. (image-dired-insert-image file
  545. ;; TODO: this should depend on the real file type
  546. (if (memq image-dired-thumbnail-storage
  547. '(standard standard-large))
  548. 'png 'jpeg)
  549. image-dired-thumb-relief
  550. image-dired-thumb-margin)
  551. (setq end (point))
  552. (add-text-properties
  553. beg end
  554. (list 'image-dired-thumbnail t
  555. 'original-file-name original-file-name
  556. 'associated-dired-buffer associated-dired-buffer
  557. 'tags (image-dired-list-tags original-file-name)
  558. 'mouse-face 'highlight
  559. 'comment (image-dired-get-comment original-file-name)))))
  560. (defun image-dired-thumb-name (file)
  561. "Return thumbnail file name for FILE.
  562. Depending on the value of `image-dired-thumbnail-storage', the file
  563. name will vary. For central thumbnail file storage, make a
  564. MD5-hash of the image file's directory name and add that to make
  565. the thumbnail file name unique. For per-directory storage, just
  566. add a subdirectory. For standard storage, produce the file name
  567. according to the Thumbnail Managing Standard."
  568. (cond ((memq image-dired-thumbnail-storage '(standard standard-large))
  569. (let* ((xdg (getenv "XDG_CACHE_HOME"))
  570. (dir (if (and xdg (file-name-absolute-p xdg))
  571. xdg "~/.cache"))
  572. (thumbdir (cl-case image-dired-thumbnail-storage
  573. (standard "thumbnails/normal")
  574. (standard-large "thumbnails/large"))))
  575. (expand-file-name
  576. (concat (md5 (concat "file://" (expand-file-name file))) ".png")
  577. (expand-file-name thumbdir dir))))
  578. ((eq 'use-image-dired-dir image-dired-thumbnail-storage)
  579. (let* ((f (expand-file-name file))
  580. (md5-hash
  581. ;; Is MD5 hashes fast enough? The checksum of a
  582. ;; thumbnail file name need not be that
  583. ;; "cryptographically" good so a faster one could
  584. ;; be used here.
  585. (md5 (file-name-as-directory (file-name-directory f)))))
  586. (format "%s%s%s.thumb.%s"
  587. (file-name-as-directory (expand-file-name (image-dired-dir)))
  588. (file-name-base f)
  589. (if md5-hash (concat "_" md5-hash) "")
  590. (file-name-extension f))))
  591. ((eq 'per-directory image-dired-thumbnail-storage)
  592. (let ((f (expand-file-name file)))
  593. (format "%s.image-dired/%s.thumb.%s"
  594. (file-name-directory f)
  595. (file-name-base f)
  596. (file-name-extension f))))))
  597. (defun image-dired--check-executable-exists (executable)
  598. (unless (executable-find (symbol-value executable))
  599. (error "Executable %S not found" executable)))
  600. (defun image-dired-thumb-size (dimension)
  601. "Return thumb size depending on `image-dired-thumbnail-storage'.
  602. DIMENSION should be either the symbol 'width or 'height."
  603. (cond
  604. ((eq 'standard image-dired-thumbnail-storage) 128)
  605. ((eq 'standard-large image-dired-thumbnail-storage) 256)
  606. (t (cl-ecase dimension
  607. (width image-dired-thumb-width)
  608. (height image-dired-thumb-height)))))
  609. (defvar image-dired-queue nil
  610. "List of items in the queue.
  611. Each item has the form (ORIGINAL-FILE TARGET-FILE).")
  612. (defvar image-dired-queue-active-jobs 0
  613. "Number of active jobs in `image-dired-queue'.")
  614. (defvar image-dired-queue-active-limit 2
  615. "Maximum number of concurrent jobs permitted for generating images.
  616. Increase at own risk.")
  617. (defun image-dired-pngnq-thumb (spec)
  618. "Quantize thumbnail described by format SPEC with pngnq(1)."
  619. (let ((process
  620. (apply #'start-process "image-dired-pngnq" nil
  621. image-dired-cmd-pngnq-program
  622. (mapcar (lambda (arg) (format-spec arg spec))
  623. image-dired-cmd-pngnq-options))))
  624. (setf (process-sentinel process)
  625. (lambda (process status)
  626. (if (and (eq (process-status process) 'exit)
  627. (zerop (process-exit-status process)))
  628. ;; Pass off to pngcrush, or just rename the
  629. ;; THUMB-nq8.png file back to THUMB.png
  630. (if (and image-dired-cmd-pngcrush-program
  631. (executable-find image-dired-cmd-pngcrush-program))
  632. (image-dired-pngcrush-thumb spec)
  633. (let ((nq8 (cdr (assq ?q spec)))
  634. (thumb (cdr (assq ?t spec))))
  635. (rename-file nq8 thumb t)))
  636. (message "command %S %s" (process-command process)
  637. (replace-regexp-in-string "\n" "" status)))))
  638. process))
  639. (defun image-dired-pngcrush-thumb (spec)
  640. "Optimize thumbnail described by format SPEC with pngcrush(1)."
  641. ;; If pngnq wasn't run, then the THUMB-nq8.png file does not exist.
  642. ;; pngcrush needs an infile and outfile, so we just copy THUMB to
  643. ;; THUMB-nq8.png and use the latter as a temp file.
  644. (when (not image-dired-cmd-pngnq-program)
  645. (let ((temp (cdr (assq ?q spec)))
  646. (thumb (cdr (assq ?t spec))))
  647. (copy-file thumb temp)))
  648. (let ((process
  649. (apply #'start-process "image-dired-pngcrush" nil
  650. image-dired-cmd-pngcrush-program
  651. (mapcar (lambda (arg) (format-spec arg spec))
  652. image-dired-cmd-pngcrush-options))))
  653. (setf (process-sentinel process)
  654. (lambda (process status)
  655. (unless (and (eq (process-status process) 'exit)
  656. (zerop (process-exit-status process)))
  657. (message "command %S %s" (process-command process)
  658. (replace-regexp-in-string "\n" "" status)))
  659. (when (memq (process-status process) '(exit signal))
  660. (let ((temp (cdr (assq ?q spec))))
  661. (delete-file temp)))))
  662. process))
  663. (defun image-dired-optipng-thumb (spec)
  664. "Optimize thumbnail described by format SPEC with optipng(1)."
  665. (let ((process
  666. (apply #'start-process "image-dired-optipng" nil
  667. image-dired-cmd-optipng-program
  668. (mapcar (lambda (arg) (format-spec arg spec))
  669. image-dired-cmd-optipng-options))))
  670. (setf (process-sentinel process)
  671. (lambda (process status)
  672. (unless (and (eq (process-status process) 'exit)
  673. (zerop (process-exit-status process)))
  674. (message "command %S %s" (process-command process)
  675. (replace-regexp-in-string "\n" "" status)))))
  676. process))
  677. (defun image-dired-create-thumb-1 (original-file thumbnail-file)
  678. "For ORIGINAL-FILE, create thumbnail image named THUMBNAIL-FILE."
  679. (image-dired--check-executable-exists
  680. 'image-dired-cmd-create-thumbnail-program)
  681. (let* ((width (int-to-string (image-dired-thumb-size 'width)))
  682. (height (int-to-string (image-dired-thumb-size 'height)))
  683. (modif-time (floor (float-time (nth 5 (file-attributes original-file)))))
  684. (thumbnail-nq8-file (replace-regexp-in-string ".png\\'" "-nq8.png"
  685. thumbnail-file))
  686. (spec
  687. (list
  688. (cons ?w width)
  689. (cons ?h height)
  690. (cons ?m modif-time)
  691. (cons ?f original-file)
  692. (cons ?q thumbnail-nq8-file)
  693. (cons ?t thumbnail-file)))
  694. (thumbnail-dir (file-name-directory thumbnail-file))
  695. process)
  696. (when (not (file-exists-p thumbnail-dir))
  697. (message "Creating thumbnail directory")
  698. (make-directory thumbnail-dir t)
  699. (set-file-modes thumbnail-dir #o700))
  700. ;; Thumbnail file creation processes begin here and are marshaled
  701. ;; in a queue by `image-dired-create-thumb'.
  702. (setq process
  703. (apply #'start-process "image-dired-create-thumbnail" nil
  704. image-dired-cmd-create-thumbnail-program
  705. (mapcar
  706. (lambda (arg) (format-spec arg spec))
  707. (if (memq image-dired-thumbnail-storage
  708. '(standard standard-large))
  709. image-dired-cmd-create-standard-thumbnail-options
  710. image-dired-cmd-create-thumbnail-options))))
  711. (setf (process-sentinel process)
  712. (lambda (process status)
  713. ;; Trigger next in queue once a thumbnail has been created
  714. (cl-decf image-dired-queue-active-jobs)
  715. (image-dired-thumb-queue-run)
  716. (if (not (and (eq (process-status process) 'exit)
  717. (zerop (process-exit-status process))))
  718. (message "Thumb could not be created for %s: %s"
  719. (abbreviate-file-name original-file)
  720. (replace-regexp-in-string "\n" "" status))
  721. (set-file-modes thumbnail-file #o600)
  722. (clear-image-cache thumbnail-file)
  723. ;; PNG thumbnail has been created since we are
  724. ;; following the XDG thumbnail spec, so try to optimize
  725. (when (memq image-dired-thumbnail-storage
  726. '(standard standard-large))
  727. (cond
  728. ((and image-dired-cmd-pngnq-program
  729. (executable-find image-dired-cmd-pngnq-program))
  730. (image-dired-pngnq-thumb spec))
  731. ((and image-dired-cmd-pngcrush-program
  732. (executable-find image-dired-cmd-pngcrush-program))
  733. (image-dired-pngcrush-thumb spec))
  734. ((and image-dired-cmd-optipng-program
  735. (executable-find image-dired-cmd-optipng-program))
  736. (image-dired-optipng-thumb spec)))))))
  737. process))
  738. (defun image-dired-thumb-queue-run ()
  739. "Run a queued job if one exists and not too many jobs are running.
  740. Queued items live in `image-dired-queue'."
  741. (while (and image-dired-queue
  742. (< image-dired-queue-active-jobs
  743. image-dired-queue-active-limit))
  744. (cl-incf image-dired-queue-active-jobs)
  745. (apply #'image-dired-create-thumb-1 (pop image-dired-queue))))
  746. (defun image-dired-create-thumb (original-file thumbnail-file)
  747. "Add a job for generating thumbnail to `image-dired-queue'."
  748. (setq image-dired-queue
  749. (nconc image-dired-queue
  750. (list (list original-file thumbnail-file))))
  751. (run-at-time 0 nil #'image-dired-thumb-queue-run))
  752. ;;;###autoload
  753. (defun image-dired-dired-toggle-marked-thumbs (&optional arg)
  754. "Toggle thumbnails in front of file names in the dired buffer.
  755. If no marked file could be found, insert or hide thumbnails on the
  756. current line. ARG, if non-nil, specifies the files to use instead
  757. of the marked files. If ARG is an integer, use the next ARG (or
  758. previous -ARG, if ARG<0) files."
  759. (interactive "P")
  760. (dired-map-over-marks
  761. (let ((image-pos (dired-move-to-filename))
  762. (image-file (dired-get-filename nil t))
  763. thumb-file
  764. overlay)
  765. (when (and image-file
  766. (string-match-p (image-file-name-regexp) image-file))
  767. (setq thumb-file (image-dired-get-thumbnail-image image-file))
  768. ;; If image is not already added, then add it.
  769. (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
  770. if (overlay-get ov 'thumb-file) return ov)))
  771. (if thumb-ov
  772. (delete-overlay thumb-ov)
  773. (put-image thumb-file image-pos)
  774. (setq overlay
  775. (cl-loop for ov in (overlays-in (point) (1+ (point)))
  776. if (overlay-get ov 'put-image) return ov))
  777. (overlay-put overlay 'image-file image-file)
  778. (overlay-put overlay 'thumb-file thumb-file)))))
  779. arg ; Show or hide image on ARG next files.
  780. 'show-progress) ; Update dired display after each image is updated.
  781. (add-hook 'dired-after-readin-hook
  782. 'image-dired-dired-after-readin-hook nil t))
  783. (defun image-dired-dired-after-readin-hook ()
  784. "Relocate existing thumbnail overlays in dired buffer after reverting.
  785. Move them to their corresponding files if they still exist.
  786. Otherwise, delete overlays."
  787. (mapc (lambda (overlay)
  788. (when (overlay-get overlay 'put-image)
  789. (let* ((image-file (overlay-get overlay 'image-file))
  790. (image-pos (dired-goto-file image-file)))
  791. (if image-pos
  792. (move-overlay overlay image-pos image-pos)
  793. (delete-overlay overlay)))))
  794. (overlays-in (point-min) (point-max))))
  795. (defun image-dired-next-line-and-display ()
  796. "Move to next dired line and display thumbnail image."
  797. (interactive)
  798. (dired-next-line 1)
  799. (image-dired-display-thumbs
  800. t (or image-dired-append-when-browsing nil) t)
  801. (if image-dired-dired-disp-props
  802. (image-dired-dired-display-properties)))
  803. (defun image-dired-previous-line-and-display ()
  804. "Move to previous dired line and display thumbnail image."
  805. (interactive)
  806. (dired-previous-line 1)
  807. (image-dired-display-thumbs
  808. t (or image-dired-append-when-browsing nil) t)
  809. (if image-dired-dired-disp-props
  810. (image-dired-dired-display-properties)))
  811. (defun image-dired-toggle-append-browsing ()
  812. "Toggle `image-dired-append-when-browsing'."
  813. (interactive)
  814. (setq image-dired-append-when-browsing
  815. (not image-dired-append-when-browsing))
  816. (message "Append browsing %s."
  817. (if image-dired-append-when-browsing
  818. "on"
  819. "off")))
  820. (defun image-dired-mark-and-display-next ()
  821. "Mark current file in dired and display next thumbnail image."
  822. (interactive)
  823. (dired-mark 1)
  824. (image-dired-display-thumbs
  825. t (or image-dired-append-when-browsing nil) t)
  826. (if image-dired-dired-disp-props
  827. (image-dired-dired-display-properties)))
  828. (defun image-dired-toggle-dired-display-properties ()
  829. "Toggle `image-dired-dired-disp-props'."
  830. (interactive)
  831. (setq image-dired-dired-disp-props
  832. (not image-dired-dired-disp-props))
  833. (message "Dired display properties %s."
  834. (if image-dired-dired-disp-props
  835. "on"
  836. "off")))
  837. (defvar image-dired-thumbnail-buffer "*image-dired*"
  838. "Image-Dired's thumbnail buffer.")
  839. (defun image-dired-create-thumbnail-buffer ()
  840. "Create thumb buffer and set `image-dired-thumbnail-mode'."
  841. (let ((buf (get-buffer-create image-dired-thumbnail-buffer)))
  842. (with-current-buffer buf
  843. (setq buffer-read-only t)
  844. (if (not (eq major-mode 'image-dired-thumbnail-mode))
  845. (image-dired-thumbnail-mode)))
  846. buf))
  847. (defvar image-dired-display-image-buffer "*image-dired-display-image*"
  848. "Where larger versions of the images are display.")
  849. (defun image-dired-create-display-image-buffer ()
  850. "Create image display buffer and set `image-dired-display-image-mode'."
  851. (let ((buf (get-buffer-create image-dired-display-image-buffer)))
  852. (with-current-buffer buf
  853. (setq buffer-read-only t)
  854. (if (not (eq major-mode 'image-dired-display-image-mode))
  855. (image-dired-display-image-mode)))
  856. buf))
  857. (defvar image-dired-saved-window-configuration nil
  858. "Saved window configuration.")
  859. ;;;###autoload
  860. (defun image-dired-dired-with-window-configuration (dir &optional arg)
  861. "Open directory DIR and create a default window configuration.
  862. Convenience command that:
  863. - Opens dired in folder DIR
  864. - Splits windows in most useful (?) way
  865. - Set `truncate-lines' to t
  866. After the command has finished, you would typically mark some
  867. image files in dired and type
  868. \\[image-dired-display-thumbs] (`image-dired-display-thumbs').
  869. If called with prefix argument ARG, skip splitting of windows.
  870. The current window configuration is saved and can be restored by
  871. calling `image-dired-restore-window-configuration'."
  872. (interactive "DDirectory: \nP")
  873. (let ((buf (image-dired-create-thumbnail-buffer))
  874. (buf2 (image-dired-create-display-image-buffer)))
  875. (setq image-dired-saved-window-configuration
  876. (current-window-configuration))
  877. (dired dir)
  878. (delete-other-windows)
  879. (when (not arg)
  880. (split-window-right)
  881. (setq truncate-lines t)
  882. (save-excursion
  883. (other-window 1)
  884. (pop-to-buffer-same-window buf)
  885. (select-window (split-window-below))
  886. (pop-to-buffer-same-window buf2)
  887. (other-window -2)))))
  888. (defun image-dired-restore-window-configuration ()
  889. "Restore window configuration.
  890. Restore any changes to the window configuration made by calling
  891. `image-dired-dired-with-window-configuration'."
  892. (interactive)
  893. (if image-dired-saved-window-configuration
  894. (set-window-configuration image-dired-saved-window-configuration)
  895. (message "No saved window configuration")))
  896. ;;;###autoload
  897. (defun image-dired-display-thumbs (&optional arg append do-not-pop)
  898. "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
  899. If a thumbnail image does not exist for a file, it is created on the
  900. fly. With prefix argument ARG, display only thumbnail for file at
  901. point (this is useful if you have marked some files but want to show
  902. another one).
  903. Recommended usage is to split the current frame horizontally so that
  904. you have the dired buffer in the left window and the
  905. `image-dired-thumbnail-buffer' buffer in the right window.
  906. With optional argument APPEND, append thumbnail to thumbnail buffer
  907. instead of erasing it first.
  908. Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
  909. used or not. If non-nil, use `display-buffer' instead of
  910. `pop-to-buffer'. This is used from functions like
  911. `image-dired-next-line-and-display' and
  912. `image-dired-previous-line-and-display' where we do not want the
  913. thumbnail buffer to be selected."
  914. (interactive "P")
  915. (let ((buf (image-dired-create-thumbnail-buffer))
  916. thumb-name files dired-buf)
  917. (if arg
  918. (setq files (list (dired-get-filename)))
  919. (setq files (dired-get-marked-files)))
  920. (setq dired-buf (current-buffer))
  921. (with-current-buffer buf
  922. (let ((inhibit-read-only t))
  923. (if (not append)
  924. (erase-buffer)
  925. (goto-char (point-max)))
  926. (dolist (curr-file files)
  927. (setq thumb-name (image-dired-thumb-name curr-file))
  928. (when (not (file-exists-p thumb-name))
  929. (image-dired-create-thumb curr-file thumb-name))
  930. (image-dired-insert-thumbnail thumb-name curr-file dired-buf)))
  931. (if do-not-pop
  932. (display-buffer buf)
  933. (pop-to-buffer buf))
  934. (cond ((eq 'dynamic image-dired-line-up-method)
  935. (image-dired-line-up-dynamic))
  936. ((eq 'fixed image-dired-line-up-method)
  937. (image-dired-line-up))
  938. ((eq 'interactive image-dired-line-up-method)
  939. (image-dired-line-up-interactive))
  940. ((eq 'none image-dired-line-up-method)
  941. nil)
  942. (t
  943. (image-dired-line-up-dynamic))))))
  944. ;;;###autoload
  945. (defun image-dired-show-all-from-dir (dir)
  946. "Make a preview buffer for all images in DIR and display it.
  947. If the number of files in DIR matching `image-file-name-regexp'
  948. exceeds `image-dired-show-all-from-dir-max-files', a warning will be
  949. displayed."
  950. (interactive "DImage Dired: ")
  951. (dired dir)
  952. (dired-mark-files-regexp (image-file-name-regexp))
  953. (let ((files (dired-get-marked-files)))
  954. (if (or (<= (length files) image-dired-show-all-from-dir-max-files)
  955. (and (> (length files) image-dired-show-all-from-dir-max-files)
  956. (y-or-n-p
  957. (format
  958. "Directory contains more than %d image files. Proceed? "
  959. image-dired-show-all-from-dir-max-files))))
  960. (progn
  961. (image-dired-display-thumbs)
  962. (pop-to-buffer image-dired-thumbnail-buffer))
  963. (message "Canceled."))))
  964. ;;;###autoload
  965. (defalias 'image-dired 'image-dired-show-all-from-dir)
  966. ;;;###autoload
  967. (define-obsolete-function-alias 'tumme 'image-dired "24.4")
  968. (defun image-dired-sane-db-file ()
  969. "Check if `image-dired-db-file' exists.
  970. If not, try to create it (including any parent directories).
  971. Signal error if there are problems creating it."
  972. (or (file-exists-p image-dired-db-file)
  973. (let (dir buf)
  974. (unless (file-directory-p (setq dir (file-name-directory
  975. image-dired-db-file)))
  976. (make-directory dir t))
  977. (with-current-buffer (setq buf (create-file-buffer
  978. image-dired-db-file))
  979. (write-file image-dired-db-file))
  980. (kill-buffer buf)
  981. (file-exists-p image-dired-db-file))
  982. (error "Could not create %s" image-dired-db-file)))
  983. (defun image-dired-write-tags (file-tags)
  984. "Write file tags to database.
  985. Write each file and tag in FILE-TAGS to the database.
  986. FILE-TAGS is an alist in the following form:
  987. ((FILE . TAG) ... )"
  988. (image-dired-sane-db-file)
  989. (let (end file tag)
  990. (image-dired--with-db-file
  991. (setq buffer-file-name image-dired-db-file)
  992. (dolist (elt file-tags)
  993. (setq file (car elt)
  994. tag (cdr elt))
  995. (goto-char (point-min))
  996. (if (search-forward-regexp (format "^%s.*$" file) nil t)
  997. (progn
  998. (setq end (point))
  999. (beginning-of-line)
  1000. (when (not (search-forward (format ";%s" tag) end t))
  1001. (end-of-line)
  1002. (insert (format ";%s" tag))))
  1003. (goto-char (point-max))
  1004. (insert (format "\n%s;%s" file tag))))
  1005. (save-buffer))))
  1006. (defun image-dired-remove-tag (files tag)
  1007. "For all FILES, remove TAG from the image database."
  1008. (image-dired-sane-db-file)
  1009. (image-dired--with-db-file
  1010. (setq buffer-file-name image-dired-db-file)
  1011. (let (end)
  1012. (unless (listp files)
  1013. (if (stringp files)
  1014. (setq files (list files))
  1015. (error "Files must be a string or a list of strings!")))
  1016. (dolist (file files)
  1017. (goto-char (point-min))
  1018. (when (search-forward-regexp (format "^%s" file) nil t)
  1019. (end-of-line)
  1020. (setq end (point))
  1021. (beginning-of-line)
  1022. (when (search-forward-regexp (format "\\(;%s\\)" tag) end t)
  1023. (delete-region (match-beginning 1) (match-end 1))
  1024. ;; Check if file should still be in the database. If
  1025. ;; it has no tags or comments, it will be removed.
  1026. (end-of-line)
  1027. (setq end (point))
  1028. (beginning-of-line)
  1029. (when (not (search-forward ";" end t))
  1030. (kill-line 1)
  1031. ;; If on empty line at end of buffer
  1032. (and (eobp)
  1033. (looking-at "^$")
  1034. (delete-char -1)))))))
  1035. (save-buffer)))
  1036. (defun image-dired-list-tags (file)
  1037. "Read all tags for image FILE from the image database."
  1038. (image-dired-sane-db-file)
  1039. (image-dired--with-db-file
  1040. (let (end (tags ""))
  1041. (when (search-forward-regexp (format "^%s" file) nil t)
  1042. (end-of-line)
  1043. (setq end (point))
  1044. (beginning-of-line)
  1045. (if (search-forward ";" end t)
  1046. (if (search-forward "comment:" end t)
  1047. (if (search-forward ";" end t)
  1048. (setq tags (buffer-substring (point) end)))
  1049. (setq tags (buffer-substring (point) end)))))
  1050. (split-string tags ";"))))
  1051. ;;;###autoload
  1052. (defun image-dired-tag-files (arg)
  1053. "Tag marked file(s) in dired. With prefix ARG, tag file at point."
  1054. (interactive "P")
  1055. (let ((tag (read-string "Tags to add (separate tags with a semicolon): "))
  1056. files)
  1057. (if arg
  1058. (setq files (list (dired-get-filename)))
  1059. (setq files (dired-get-marked-files)))
  1060. (image-dired-write-tags
  1061. (mapcar
  1062. (lambda (x)
  1063. (cons x tag))
  1064. files))))
  1065. (defun image-dired-tag-thumbnail ()
  1066. "Tag current thumbnail."
  1067. (interactive)
  1068. (let ((tag (read-string "Tags to add (separate tags with a semicolon): ")))
  1069. (image-dired-write-tags (list (cons (image-dired-original-file-name) tag))))
  1070. (image-dired-update-property
  1071. 'tags (image-dired-list-tags (image-dired-original-file-name))))
  1072. ;;;###autoload
  1073. (defun image-dired-delete-tag (arg)
  1074. "Remove tag for selected file(s).
  1075. With prefix argument ARG, remove tag from file at point."
  1076. (interactive "P")
  1077. (let ((tag (read-string "Tag to remove: "))
  1078. files)
  1079. (if arg
  1080. (setq files (list (dired-get-filename)))
  1081. (setq files (dired-get-marked-files)))
  1082. (image-dired-remove-tag files tag)))
  1083. (defun image-dired-tag-thumbnail-remove ()
  1084. "Remove tag from thumbnail."
  1085. (interactive)
  1086. (let ((tag (read-string "Tag to remove: ")))
  1087. (image-dired-remove-tag (image-dired-original-file-name) tag))
  1088. (image-dired-update-property
  1089. 'tags (image-dired-list-tags (image-dired-original-file-name))))
  1090. (defun image-dired-original-file-name ()
  1091. "Get original file name for thumbnail or display image at point."
  1092. (get-text-property (point) 'original-file-name))
  1093. (defun image-dired-file-name-at-point ()
  1094. "Get abbreviated file name for thumbnail or display image at point."
  1095. (let ((f (image-dired-original-file-name)))
  1096. (when f
  1097. (abbreviate-file-name f))))
  1098. (defun image-dired-associated-dired-buffer ()
  1099. "Get associated dired buffer at point."
  1100. (get-text-property (point) 'associated-dired-buffer))
  1101. (defun image-dired-get-buffer-window (buf)
  1102. "Return window where buffer BUF is."
  1103. (get-window-with-predicate
  1104. (lambda (window)
  1105. (equal (window-buffer window) buf))
  1106. nil t))
  1107. (defun image-dired-track-original-file ()
  1108. "Track the original file in the associated dired buffer.
  1109. See documentation for `image-dired-toggle-movement-tracking'.
  1110. Interactive use only useful if `image-dired-track-movement' is nil."
  1111. (interactive)
  1112. (let* ((dired-buf (image-dired-associated-dired-buffer))
  1113. (file-name (image-dired-original-file-name))
  1114. (window (image-dired-get-buffer-window dired-buf)))
  1115. (and (buffer-live-p dired-buf) file-name
  1116. (with-current-buffer dired-buf
  1117. (if (not (dired-goto-file file-name))
  1118. (message "Could not track file")
  1119. (if window (set-window-point window (point))))))))
  1120. (defun image-dired-toggle-movement-tracking ()
  1121. "Turn on and off `image-dired-track-movement'.
  1122. Tracking of the movements between thumbnail and dired buffer so that
  1123. they are \"mirrored\" in the dired buffer. When this is on, moving
  1124. around in the thumbnail or dired buffer will find the matching
  1125. position in the other buffer."
  1126. (interactive)
  1127. (setq image-dired-track-movement (not image-dired-track-movement))
  1128. (message "Tracking %s" (if image-dired-track-movement "on" "off")))
  1129. (defun image-dired-track-thumbnail ()
  1130. "Track current dired file's thumb in `image-dired-thumbnail-buffer'.
  1131. This is almost the same as what `image-dired-track-original-file' does,
  1132. but the other way around."
  1133. (let ((file (dired-get-filename))
  1134. prop-val found window)
  1135. (when (get-buffer image-dired-thumbnail-buffer)
  1136. (with-current-buffer image-dired-thumbnail-buffer
  1137. (goto-char (point-min))
  1138. (while (and (not (eobp))
  1139. (not found))
  1140. (if (and (setq prop-val
  1141. (get-text-property (point) 'original-file-name))
  1142. (string= prop-val file))
  1143. (setq found t))
  1144. (if (not found)
  1145. (forward-char 1)))
  1146. (when found
  1147. (if (setq window (image-dired-thumbnail-window))
  1148. (set-window-point window (point)))
  1149. (image-dired-display-thumb-properties))))))
  1150. (defun image-dired-dired-next-line (&optional arg)
  1151. "Call `dired-next-line', then track thumbnail.
  1152. This can safely replace `dired-next-line'.
  1153. With prefix argument, move ARG lines."
  1154. (interactive "P")
  1155. (dired-next-line (or arg 1))
  1156. (if image-dired-track-movement
  1157. (image-dired-track-thumbnail)))
  1158. (defun image-dired-dired-previous-line (&optional arg)
  1159. "Call `dired-previous-line', then track thumbnail.
  1160. This can safely replace `dired-previous-line'.
  1161. With prefix argument, move ARG lines."
  1162. (interactive "P")
  1163. (dired-previous-line (or arg 1))
  1164. (if image-dired-track-movement
  1165. (image-dired-track-thumbnail)))
  1166. (defun image-dired-forward-image (&optional arg)
  1167. "Move to next image and display properties.
  1168. Optional prefix ARG says how many images to move; default is one
  1169. image."
  1170. (interactive "p")
  1171. (let (pos (steps (or arg 1)))
  1172. (dotimes (_ steps)
  1173. (if (and (not (eobp))
  1174. (save-excursion
  1175. (forward-char)
  1176. (while (and (not (eobp))
  1177. (not (image-dired-image-at-point-p)))
  1178. (forward-char))
  1179. (setq pos (point))
  1180. (image-dired-image-at-point-p)))
  1181. (goto-char pos)
  1182. (error "At last image"))))
  1183. (when image-dired-track-movement
  1184. (image-dired-track-original-file))
  1185. (image-dired-display-thumb-properties))
  1186. (defun image-dired-backward-image (&optional arg)
  1187. "Move to previous image and display properties.
  1188. Optional prefix ARG says how many images to move; default is one
  1189. image."
  1190. (interactive "p")
  1191. (let (pos (steps (or arg 1)))
  1192. (dotimes (_ steps)
  1193. (if (and (not (bobp))
  1194. (save-excursion
  1195. (backward-char)
  1196. (while (and (not (bobp))
  1197. (not (image-dired-image-at-point-p)))
  1198. (backward-char))
  1199. (setq pos (point))
  1200. (image-dired-image-at-point-p)))
  1201. (goto-char pos)
  1202. (error "At first image"))))
  1203. (when image-dired-track-movement
  1204. (image-dired-track-original-file))
  1205. (image-dired-display-thumb-properties))
  1206. (defun image-dired-next-line ()
  1207. "Move to next line and display properties."
  1208. (interactive)
  1209. (let ((goal-column (current-column)))
  1210. (forward-line 1)
  1211. (move-to-column goal-column))
  1212. ;; If we end up in an empty spot, back up to the next thumbnail.
  1213. (if (not (image-dired-image-at-point-p))
  1214. (image-dired-backward-image))
  1215. (if image-dired-track-movement
  1216. (image-dired-track-original-file))
  1217. (image-dired-display-thumb-properties))
  1218. (defun image-dired-previous-line ()
  1219. "Move to previous line and display properties."
  1220. (interactive)
  1221. (let ((goal-column (current-column)))
  1222. (forward-line -1)
  1223. (move-to-column goal-column))
  1224. ;; If we end up in an empty spot, back up to the next
  1225. ;; thumbnail. This should only happen if the user deleted a
  1226. ;; thumbnail and did not refresh, so it is not very common. But we
  1227. ;; can handle it in a good manner, so why not?
  1228. (if (not (image-dired-image-at-point-p))
  1229. (image-dired-backward-image))
  1230. (if image-dired-track-movement
  1231. (image-dired-track-original-file))
  1232. (image-dired-display-thumb-properties))
  1233. (defun image-dired-format-properties-string (buf file props comment)
  1234. "Format display properties.
  1235. BUF is the associated dired buffer, FILE is the original image file
  1236. name, PROPS is a stringified list of tags and COMMENT is the image file's
  1237. comment."
  1238. (format-spec
  1239. image-dired-display-properties-format
  1240. (list
  1241. (cons ?b (or buf ""))
  1242. (cons ?f file)
  1243. (cons ?t (or props ""))
  1244. (cons ?c (or comment "")))))
  1245. (defun image-dired-display-thumb-properties ()
  1246. "Display thumbnail properties in the echo area."
  1247. (if (not (eobp))
  1248. (let ((file-name (file-name-nondirectory (image-dired-original-file-name)))
  1249. (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
  1250. (props (mapconcat #'identity (get-text-property (point) 'tags) ", "))
  1251. (comment (get-text-property (point) 'comment))
  1252. (message-log-max nil))
  1253. (if file-name
  1254. (message "%s"
  1255. (image-dired-format-properties-string
  1256. dired-buf
  1257. file-name
  1258. props
  1259. comment))))))
  1260. (defun image-dired-dired-file-marked-p ()
  1261. "Check whether file on current line is marked or not."
  1262. (save-excursion
  1263. (beginning-of-line)
  1264. (not (looking-at "^ .*$"))))
  1265. (defun image-dired-modify-mark-on-thumb-original-file (command)
  1266. "Modify mark in dired buffer.
  1267. COMMAND is one of `mark' for marking file in dired, `unmark' for
  1268. unmarking file in dired or `flag' for flagging file for delete in
  1269. dired."
  1270. (let ((file-name (image-dired-original-file-name))
  1271. (dired-buf (image-dired-associated-dired-buffer)))
  1272. (if (not (and dired-buf file-name))
  1273. (message "No image, or image with correct properties, at point.")
  1274. (with-current-buffer dired-buf
  1275. (message "%s" file-name)
  1276. (if (dired-goto-file file-name)
  1277. (cond ((eq command 'mark) (dired-mark 1))
  1278. ((eq command 'unmark) (dired-unmark 1))
  1279. ((eq command 'toggle)
  1280. (if (image-dired-dired-file-marked-p)
  1281. (dired-unmark 1)
  1282. (dired-mark 1)))
  1283. ((eq command 'flag) (dired-flag-file-deletion 1))))))))
  1284. (defun image-dired-mark-thumb-original-file ()
  1285. "Mark original image file in associated dired buffer."
  1286. (interactive)
  1287. (image-dired-modify-mark-on-thumb-original-file 'mark)
  1288. (image-dired-forward-image))
  1289. (defun image-dired-unmark-thumb-original-file ()
  1290. "Unmark original image file in associated dired buffer."
  1291. (interactive)
  1292. (image-dired-modify-mark-on-thumb-original-file 'unmark)
  1293. (image-dired-forward-image))
  1294. (defun image-dired-flag-thumb-original-file ()
  1295. "Flag original image file for deletion in associated dired buffer."
  1296. (interactive)
  1297. (image-dired-modify-mark-on-thumb-original-file 'flag)
  1298. (image-dired-forward-image))
  1299. (defun image-dired-toggle-mark-thumb-original-file ()
  1300. "Toggle mark on original image file in associated dired buffer."
  1301. (interactive)
  1302. (image-dired-modify-mark-on-thumb-original-file 'toggle))
  1303. (defun image-dired-jump-original-dired-buffer ()
  1304. "Jump to the dired buffer associated with the current image file.
  1305. You probably want to use this together with
  1306. `image-dired-track-original-file'."
  1307. (interactive)
  1308. (let ((buf (image-dired-associated-dired-buffer))
  1309. window frame)
  1310. (setq window (image-dired-get-buffer-window buf))
  1311. (if window
  1312. (progn
  1313. (if (not (equal (selected-frame) (setq frame (window-frame window))))
  1314. (select-frame-set-input-focus frame))
  1315. (select-window window))
  1316. (message "Associated dired buffer not visible"))))
  1317. ;;;###autoload
  1318. (defun image-dired-jump-thumbnail-buffer ()
  1319. "Jump to thumbnail buffer."
  1320. (interactive)
  1321. (let ((window (image-dired-thumbnail-window))
  1322. frame)
  1323. (if window
  1324. (progn
  1325. (if (not (equal (selected-frame) (setq frame (window-frame window))))
  1326. (select-frame-set-input-focus frame))
  1327. (select-window window))
  1328. (message "Thumbnail buffer not visible"))))
  1329. (defvar image-dired-thumbnail-mode-line-up-map
  1330. (let ((map (make-sparse-keymap)))
  1331. ;; map it to "g" so that the user can press it more quickly
  1332. (define-key map "g" 'image-dired-line-up-dynamic)
  1333. ;; "f" for "fixed" number of thumbs per row
  1334. (define-key map "f" 'image-dired-line-up)
  1335. ;; "i" for "interactive"
  1336. (define-key map "i" 'image-dired-line-up-interactive)
  1337. map)
  1338. "Keymap for line-up commands in `image-dired-thumbnail-mode'.")
  1339. (defvar image-dired-thumbnail-mode-tag-map
  1340. (let ((map (make-sparse-keymap)))
  1341. ;; map it to "t" so that the user can press it more quickly
  1342. (define-key map "t" 'image-dired-tag-thumbnail)
  1343. ;; "r" for "remove"
  1344. (define-key map "r" 'image-dired-tag-thumbnail-remove)
  1345. map)
  1346. "Keymap for tag commands in `image-dired-thumbnail-mode'.")
  1347. (defvar image-dired-thumbnail-mode-map
  1348. (let ((map (make-sparse-keymap)))
  1349. (define-key map [right] 'image-dired-forward-image)
  1350. (define-key map [left] 'image-dired-backward-image)
  1351. (define-key map [up] 'image-dired-previous-line)
  1352. (define-key map [down] 'image-dired-next-line)
  1353. (define-key map "\C-f" 'image-dired-forward-image)
  1354. (define-key map "\C-b" 'image-dired-backward-image)
  1355. (define-key map "\C-p" 'image-dired-previous-line)
  1356. (define-key map "\C-n" 'image-dired-next-line)
  1357. (define-key map "d" 'image-dired-flag-thumb-original-file)
  1358. (define-key map [delete] 'image-dired-flag-thumb-original-file)
  1359. (define-key map "m" 'image-dired-mark-thumb-original-file)
  1360. (define-key map "u" 'image-dired-unmark-thumb-original-file)
  1361. (define-key map "." 'image-dired-track-original-file)
  1362. (define-key map [tab] 'image-dired-jump-original-dired-buffer)
  1363. ;; add line-up map
  1364. (define-key map "g" image-dired-thumbnail-mode-line-up-map)
  1365. ;; add tag map
  1366. (define-key map "t" image-dired-thumbnail-mode-tag-map)
  1367. (define-key map "\C-m" 'image-dired-display-thumbnail-original-image)
  1368. (define-key map [C-return] 'image-dired-thumbnail-display-external)
  1369. (define-key map "l" 'image-dired-rotate-thumbnail-left)
  1370. (define-key map "r" 'image-dired-rotate-thumbnail-right)
  1371. (define-key map "L" 'image-dired-rotate-original-left)
  1372. (define-key map "R" 'image-dired-rotate-original-right)
  1373. (define-key map "D" 'image-dired-thumbnail-set-image-description)
  1374. (define-key map "\C-d" 'image-dired-delete-char)
  1375. (define-key map " " 'image-dired-display-next-thumbnail-original)
  1376. (define-key map (kbd "DEL") 'image-dired-display-previous-thumbnail-original)
  1377. (define-key map "c" 'image-dired-comment-thumbnail)
  1378. ;; Mouse
  1379. (define-key map [mouse-2] 'image-dired-mouse-display-image)
  1380. (define-key map [mouse-1] 'image-dired-mouse-select-thumbnail)
  1381. ;; Seems I must first set C-down-mouse-1 to undefined, or else it
  1382. ;; will trigger the buffer menu. If I try to instead bind
  1383. ;; C-down-mouse-1 to `image-dired-mouse-toggle-mark', I get a message
  1384. ;; about C-mouse-1 not being defined afterwards. Annoying, but I
  1385. ;; probably do not completely understand mouse events.
  1386. (define-key map [C-down-mouse-1] 'undefined)
  1387. (define-key map [C-mouse-1] 'image-dired-mouse-toggle-mark)
  1388. ;; Menu
  1389. (easy-menu-define nil map
  1390. "Menu for `image-dired-thumbnail-mode'."
  1391. '("Image-Dired"
  1392. ["Quit" quit-window]
  1393. ["Delete thumbnail from buffer" image-dired-delete-char]
  1394. ["Remove tag from thumbnail" image-dired-tag-thumbnail-remove]
  1395. ["Tag thumbnail" image-dired-tag-thumbnail]
  1396. ["Comment thumbnail" image-dired-comment-thumbnail]
  1397. ["Refresh thumb" image-dired-refresh-thumb]
  1398. ["Dynamic line up" image-dired-line-up-dynamic]
  1399. ["Line up thumbnails" image-dired-line-up]
  1400. ["Rotate thumbnail left" image-dired-rotate-thumbnail-left]
  1401. ["Rotate thumbnail right" image-dired-rotate-thumbnail-right]
  1402. ["Rotate original left" image-dired-rotate-original-left]
  1403. ["Rotate original right" image-dired-rotate-original-right]
  1404. ["Toggle movement tracking on/off" image-dired-toggle-movement-tracking]
  1405. ["Jump to dired buffer" image-dired-jump-original-dired-buffer]
  1406. ["Track original" image-dired-track-original-file]
  1407. ["Flag original for deletion" image-dired-flag-thumb-original-file]
  1408. ["Unmark original" image-dired-unmark-thumb-original-file]
  1409. ["Mark original" image-dired-mark-thumb-original-file]
  1410. ["Display in external viewer" image-dired-thumbnail-display-external]
  1411. ["Display image" image-dired-display-thumbnail-original-image]))
  1412. map)
  1413. "Keymap for `image-dired-thumbnail-mode'.")
  1414. (defvar image-dired-display-image-mode-map
  1415. (let ((map (make-sparse-keymap)))
  1416. ;; `image-mode-map' has bindings that do not make sense in image-dired
  1417. ;; (set-keymap-parent map image-mode-map)
  1418. (define-key map "f" 'image-dired-display-current-image-full)
  1419. (define-key map "s" 'image-dired-display-current-image-sized)
  1420. (define-key map "g" nil)
  1421. ;; Useful bindings from `image-mode-map'
  1422. (define-key map [remap forward-char] 'image-forward-hscroll)
  1423. (define-key map [remap backward-char] 'image-backward-hscroll)
  1424. (define-key map [remap right-char] 'image-forward-hscroll)
  1425. (define-key map [remap left-char] 'image-backward-hscroll)
  1426. (define-key map [remap previous-line] 'image-previous-line)
  1427. (define-key map [remap next-line] 'image-next-line)
  1428. (define-key map [remap scroll-up] 'image-scroll-up)
  1429. (define-key map [remap scroll-down] 'image-scroll-down)
  1430. (define-key map [remap scroll-up-command] 'image-scroll-up)
  1431. (define-key map [remap scroll-down-command] 'image-scroll-down)
  1432. (define-key map [remap scroll-left] 'image-scroll-left)
  1433. (define-key map [remap scroll-right] 'image-scroll-right)
  1434. (define-key map [remap move-beginning-of-line] 'image-bol)
  1435. (define-key map [remap move-end-of-line] 'image-eol)
  1436. (define-key map [remap beginning-of-buffer] 'image-bob)
  1437. (define-key map [remap end-of-buffer] 'image-eob)
  1438. (easy-menu-define nil map
  1439. "Menu for `image-dired-display-image-mode-map'."
  1440. '("Image-Dired"
  1441. ["Quit" quit-window]
  1442. ["Display original, sized to fit" image-dired-display-current-image-sized]
  1443. ["Display original, full size" image-dired-display-current-image-full]))
  1444. map)
  1445. "Keymap for `image-dired-display-image-mode'.")
  1446. (defun image-dired-display-current-image-full ()
  1447. "Display current image in full size."
  1448. (interactive)
  1449. (let ((file (image-dired-original-file-name)))
  1450. (if file
  1451. (progn
  1452. (image-dired-display-image file t)
  1453. (message "Full size image displayed"))
  1454. (error "No original file name at point"))))
  1455. (defun image-dired-display-current-image-sized ()
  1456. "Display current image in sized to fit window dimensions."
  1457. (interactive)
  1458. (let ((file (image-dired-original-file-name)))
  1459. (if file
  1460. (progn
  1461. (image-dired-display-image file)
  1462. (message "Fitted image displayed"))
  1463. (error "No original file name at point"))))
  1464. (define-derived-mode image-dired-thumbnail-mode
  1465. special-mode "image-dired-thumbnail"
  1466. "Browse and manipulate thumbnail images using dired.
  1467. Use `image-dired-minor-mode' to get a nice setup."
  1468. :group 'image-dired
  1469. (buffer-disable-undo)
  1470. (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
  1471. (define-derived-mode image-dired-display-image-mode
  1472. special-mode "image-dired-image-display"
  1473. "Mode for displaying and manipulating original image.
  1474. Resized or in full-size."
  1475. :group 'image-dired
  1476. (buffer-disable-undo)
  1477. (image-mode-setup-winprops)
  1478. (setq cursor-type nil)
  1479. (add-hook 'file-name-at-point-functions 'image-dired-file-name-at-point nil t))
  1480. (defvar image-dired-minor-mode-map
  1481. (let ((map (make-sparse-keymap)))
  1482. ;; (set-keymap-parent map dired-mode-map)
  1483. ;; Hijack previous and next line movement. Let C-p and C-b be
  1484. ;; though...
  1485. (define-key map "p" 'image-dired-dired-previous-line)
  1486. (define-key map "n" 'image-dired-dired-next-line)
  1487. (define-key map [up] 'image-dired-dired-previous-line)
  1488. (define-key map [down] 'image-dired-dired-next-line)
  1489. (define-key map (kbd "C-S-n") 'image-dired-next-line-and-display)
  1490. (define-key map (kbd "C-S-p") 'image-dired-previous-line-and-display)
  1491. (define-key map (kbd "C-S-m") 'image-dired-mark-and-display-next)
  1492. (define-key map "\C-td" 'image-dired-display-thumbs)
  1493. (define-key map [tab] 'image-dired-jump-thumbnail-buffer)
  1494. (define-key map "\C-ti" 'image-dired-dired-display-image)
  1495. (define-key map "\C-tx" 'image-dired-dired-display-external)
  1496. (define-key map "\C-ta" 'image-dired-display-thumbs-append)
  1497. (define-key map "\C-t." 'image-dired-display-thumb)
  1498. (define-key map "\C-tc" 'image-dired-dired-comment-files)
  1499. (define-key map "\C-tf" 'image-dired-mark-tagged-files)
  1500. ;; Menu for dired
  1501. (easy-menu-define nil map
  1502. "Menu for `image-dired-minor-mode'."
  1503. '("Image-dired"
  1504. ["Copy with EXIF file name" image-dired-copy-with-exif-file-name]
  1505. ["Comment files" image-dired-dired-comment-files]
  1506. ["Mark tagged files" image-dired-mark-tagged-files]
  1507. ["Jump to thumbnail buffer" image-dired-jump-thumbnail-buffer]
  1508. ["Toggle movement tracking" image-dired-toggle-movement-tracking]
  1509. ["Toggle append browsing" image-dired-toggle-append-browsing]
  1510. ["Toggle display properties" image-dired-toggle-dired-display-properties]
  1511. ["Display in external viewer" image-dired-dired-display-external]
  1512. ["Display image" image-dired-dired-display-image]
  1513. ["Display this thumbnail" image-dired-display-thumb]
  1514. ["Display thumbnails append" image-dired-display-thumbs-append]
  1515. ["Create thumbnails for marked files" image-dired-create-thumbs]
  1516. ["Mark and display next" image-dired-mark-and-display-next]
  1517. ["Display thumb for previous file" image-dired-previous-line-and-display]
  1518. ["Display thumb for next file" image-dired-next-line-and-display]))
  1519. map)
  1520. "Keymap for `image-dired-minor-mode'.")
  1521. ;;;###autoload
  1522. (define-minor-mode image-dired-minor-mode
  1523. "Setup easy-to-use keybindings for the commands to be used in dired mode.
  1524. Note that n, p and <down> and <up> will be hijacked and bound to
  1525. `image-dired-dired-x-line'."
  1526. :keymap image-dired-minor-mode-map)
  1527. ;;;###autoload
  1528. (define-obsolete-function-alias 'image-dired-setup-dired-keybindings 'image-dired-minor-mode
  1529. "26.1")
  1530. (declare-function clear-image-cache "image.c" (&optional filter))
  1531. (defun image-dired-create-thumbs (&optional arg)
  1532. "Create thumbnail images for all marked files in dired.
  1533. With prefix argument ARG, create thumbnails even if they already exist
  1534. \(i.e. use this to refresh your thumbnails)."
  1535. (interactive "P")
  1536. (let (thumb-name)
  1537. (dolist (curr-file (dired-get-marked-files))
  1538. (setq thumb-name (image-dired-thumb-name curr-file))
  1539. ;; If the user overrides the exist check, we must clear the
  1540. ;; image cache so that if the user wants to display the
  1541. ;; thumbnail, it is not fetched from cache.
  1542. (when arg
  1543. (clear-image-cache (expand-file-name thumb-name)))
  1544. (when (or (not (file-exists-p thumb-name))
  1545. arg)
  1546. (image-dired-create-thumb curr-file thumb-name)))))
  1547. (defvar image-dired-slideshow-timer nil
  1548. "Slideshow timer.")
  1549. (defvar image-dired-slideshow-count 0
  1550. "Keeping track on number of images in slideshow.")
  1551. (defvar image-dired-slideshow-times 0
  1552. "Number of pictures to display in slideshow.")
  1553. (defun image-dired-slideshow-step ()
  1554. "Step to next file, if `image-dired-slideshow-times' has not been reached."
  1555. (if (< image-dired-slideshow-count image-dired-slideshow-times)
  1556. (progn
  1557. (message "%s" (1+ image-dired-slideshow-count))
  1558. (setq image-dired-slideshow-count (1+ image-dired-slideshow-count))
  1559. (image-dired-next-line-and-display))
  1560. (image-dired-slideshow-stop)))
  1561. (defun image-dired-slideshow-start ()
  1562. "Start slideshow.
  1563. Ask user for number of images to show and the delay in between."
  1564. (interactive)
  1565. (setq image-dired-slideshow-count 0)
  1566. (setq image-dired-slideshow-times (string-to-number (read-string "How many: ")))
  1567. (let ((repeat (string-to-number
  1568. (read-string
  1569. "Delay, in seconds. Decimals are accepted : " "1"))))
  1570. (setq image-dired-slideshow-timer
  1571. (run-with-timer
  1572. 0 repeat
  1573. 'image-dired-slideshow-step))))
  1574. (defun image-dired-slideshow-stop ()
  1575. "Cancel slideshow."
  1576. (interactive)
  1577. (cancel-timer image-dired-slideshow-timer))
  1578. (defun image-dired-delete-char ()
  1579. "Remove current thumbnail from thumbnail buffer and line up."
  1580. (interactive)
  1581. (let ((inhibit-read-only t))
  1582. (delete-char 1)
  1583. (when (= (following-char) ?\s)
  1584. (delete-char 1))))
  1585. ;;;###autoload
  1586. (defun image-dired-display-thumbs-append ()
  1587. "Append thumbnails to `image-dired-thumbnail-buffer'."
  1588. (interactive)
  1589. (image-dired-display-thumbs nil t t))
  1590. ;;;###autoload
  1591. (defun image-dired-display-thumb ()
  1592. "Shorthand for `image-dired-display-thumbs' with prefix argument."
  1593. (interactive)
  1594. (image-dired-display-thumbs t nil t))
  1595. (defun image-dired-line-up ()
  1596. "Line up thumbnails according to `image-dired-thumbs-per-row'.
  1597. See also `image-dired-line-up-dynamic'."
  1598. (interactive)
  1599. (let ((inhibit-read-only t))
  1600. (goto-char (point-min))
  1601. (while (and (not (image-dired-image-at-point-p))
  1602. (not (eobp)))
  1603. (delete-char 1))
  1604. (while (not (eobp))
  1605. (forward-char)
  1606. (while (and (not (image-dired-image-at-point-p))
  1607. (not (eobp)))
  1608. (delete-char 1)))
  1609. (goto-char (point-min))
  1610. (let ((count 0))
  1611. (while (not (eobp))
  1612. (forward-char)
  1613. (if (= image-dired-thumbs-per-row 1)
  1614. (insert "\n")
  1615. (insert " ")
  1616. (setq count (1+ count))
  1617. (when (and (= count (- image-dired-thumbs-per-row 1))
  1618. (not (eobp)))
  1619. (forward-char)
  1620. (insert "\n")
  1621. (setq count 0)))))
  1622. (goto-char (point-min))))
  1623. (defun image-dired-line-up-dynamic ()
  1624. "Line up thumbnails images dynamically.
  1625. Calculate how many thumbnails fit."
  1626. (interactive)
  1627. (let* ((char-width (frame-char-width))
  1628. (width (image-dired-window-width-pixels (image-dired-thumbnail-window)))
  1629. (image-dired-thumbs-per-row
  1630. (/ width
  1631. (+ (* 2 image-dired-thumb-relief)
  1632. (* 2 image-dired-thumb-margin)
  1633. (image-dired-thumb-size 'width)
  1634. char-width))))
  1635. (image-dired-line-up)))
  1636. (defun image-dired-line-up-interactive ()
  1637. "Line up thumbnails interactively.
  1638. Ask user how many thumbnails should be displayed per row."
  1639. (interactive)
  1640. (let ((image-dired-thumbs-per-row
  1641. (string-to-number (read-string "How many thumbs per row: "))))
  1642. (if (not (> image-dired-thumbs-per-row 0))
  1643. (message "Number must be greater than 0")
  1644. (image-dired-line-up))))
  1645. (defun image-dired-thumbnail-display-external ()
  1646. "Display original image for thumbnail at point using external viewer."
  1647. (interactive)
  1648. (let ((file (image-dired-original-file-name)))
  1649. (if (not (image-dired-image-at-point-p))
  1650. (message "No thumbnail at point")
  1651. (if (not file)
  1652. (message "No original file name found")
  1653. (start-process "image-dired-thumb-external" nil
  1654. image-dired-external-viewer file)))))
  1655. ;;;###autoload
  1656. (defun image-dired-dired-display-external ()
  1657. "Display file at point using an external viewer."
  1658. (interactive)
  1659. (let ((file (dired-get-filename)))
  1660. (start-process "image-dired-external" nil
  1661. image-dired-external-viewer file)))
  1662. (defun image-dired-window-width-pixels (window)
  1663. "Calculate WINDOW width in pixels."
  1664. (* (window-width window) (frame-char-width)))
  1665. (defun image-dired-window-height-pixels (window)
  1666. "Calculate WINDOW height in pixels."
  1667. ;; Note: The mode-line consumes one line
  1668. (* (- (window-height window) 1) (frame-char-height)))
  1669. (defun image-dired-display-window ()
  1670. "Return window where `image-dired-display-image-buffer' is visible."
  1671. (get-window-with-predicate
  1672. (lambda (window)
  1673. (equal (buffer-name (window-buffer window)) image-dired-display-image-buffer))
  1674. nil t))
  1675. (defun image-dired-thumbnail-window ()
  1676. "Return window where `image-dired-thumbnail-buffer' is visible."
  1677. (get-window-with-predicate
  1678. (lambda (window)
  1679. (equal (buffer-name (window-buffer window)) image-dired-thumbnail-buffer))
  1680. nil t))
  1681. (defun image-dired-associated-dired-buffer-window ()
  1682. "Return window where associated dired buffer is visible."
  1683. (let (buf)
  1684. (if (image-dired-image-at-point-p)
  1685. (progn
  1686. (setq buf (image-dired-associated-dired-buffer))
  1687. (get-window-with-predicate
  1688. (lambda (window)
  1689. (equal (window-buffer window) buf))))
  1690. (error "No thumbnail image at point"))))
  1691. (defun image-dired-display-window-width (window)
  1692. "Return width, in pixels, of WINDOW."
  1693. (- (image-dired-window-width-pixels window)
  1694. image-dired-display-window-width-correction))
  1695. (defun image-dired-display-window-height (window)
  1696. "Return height, in pixels, of WINDOW."
  1697. (- (image-dired-window-height-pixels window)
  1698. image-dired-display-window-height-correction))
  1699. (defun image-dired-display-image (file &optional original-size)
  1700. "Display image FILE in image buffer.
  1701. Use this when you want to display the image, semi sized, in a new
  1702. window. The image is sized to fit the display window (using a
  1703. temporary file, don't worry). Because of this, it will not be as
  1704. quick as opening it directly, but on most modern systems it
  1705. should feel snappy enough.
  1706. If optional argument ORIGINAL-SIZE is non-nil, display image in its
  1707. original size."
  1708. (image-dired--check-executable-exists
  1709. 'image-dired-cmd-create-temp-image-program)
  1710. (let ((new-file (expand-file-name image-dired-temp-image-file))
  1711. (window (image-dired-display-window))
  1712. (image-type 'jpeg))
  1713. (setq file (expand-file-name file))
  1714. (if (not original-size)
  1715. (let* ((spec
  1716. (list
  1717. (cons ?p image-dired-cmd-create-temp-image-program)
  1718. (cons ?w (image-dired-display-window-width window))
  1719. (cons ?h (image-dired-display-window-height window))
  1720. (cons ?f file)
  1721. (cons ?t new-file)))
  1722. (ret
  1723. (apply #'call-process
  1724. image-dired-cmd-create-temp-image-program nil nil nil
  1725. (mapcar
  1726. (lambda (arg) (format-spec arg spec))
  1727. image-dired-cmd-create-temp-image-options))))
  1728. (when (not (zerop ret))
  1729. (error "Could not resize image")))
  1730. (setq image-type (image-type-from-file-name file))
  1731. (copy-file file new-file t))
  1732. (with-current-buffer (image-dired-create-display-image-buffer)
  1733. (let ((inhibit-read-only t))
  1734. (erase-buffer)
  1735. (clear-image-cache)
  1736. (image-dired-insert-image image-dired-temp-image-file image-type 0 0)
  1737. (goto-char (point-min))
  1738. (set-window-vscroll window 0)
  1739. (set-window-hscroll window 0)
  1740. (image-dired-update-property 'original-file-name file)))))
  1741. (defun image-dired-display-thumbnail-original-image (&optional arg)
  1742. "Display current thumbnail's original image in display buffer.
  1743. See documentation for `image-dired-display-image' for more information.
  1744. With prefix argument ARG, display image in its original size."
  1745. (interactive "P")
  1746. (let ((file (image-dired-original-file-name)))
  1747. (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
  1748. (message "Not in image-dired-thumbnail-mode")
  1749. (if (not (image-dired-image-at-point-p))
  1750. (message "No thumbnail at point")
  1751. (if (not file)
  1752. (message "No original file name found")
  1753. (image-dired-create-display-image-buffer)
  1754. (display-buffer image-dired-display-image-buffer)
  1755. (image-dired-display-image file arg))))))
  1756. ;;;###autoload
  1757. (defun image-dired-dired-display-image (&optional arg)
  1758. "Display current image file.
  1759. See documentation for `image-dired-display-image' for more information.
  1760. With prefix argument ARG, display image in its original size."
  1761. (interactive "P")
  1762. (image-dired-create-display-image-buffer)
  1763. (display-buffer image-dired-display-image-buffer)
  1764. (image-dired-display-image (dired-get-filename) arg))
  1765. (defun image-dired-image-at-point-p ()
  1766. "Return true if there is an image-dired thumbnail at point."
  1767. (get-text-property (point) 'image-dired-thumbnail))
  1768. (defun image-dired-rotate-thumbnail (degrees)
  1769. "Rotate thumbnail DEGREES degrees."
  1770. (image-dired--check-executable-exists
  1771. 'image-dired-cmd-rotate-thumbnail-program)
  1772. (if (not (image-dired-image-at-point-p))
  1773. (message "No thumbnail at point")
  1774. (let* ((file (image-dired-thumb-name (image-dired-original-file-name)))
  1775. (thumb (expand-file-name file))
  1776. (spec (list (cons ?d degrees) (cons ?t thumb))))
  1777. (apply #'call-process image-dired-cmd-rotate-thumbnail-program nil nil nil
  1778. (mapcar (lambda (arg) (format-spec arg spec))
  1779. image-dired-cmd-rotate-thumbnail-options))
  1780. (clear-image-cache thumb))))
  1781. (defun image-dired-rotate-thumbnail-left ()
  1782. "Rotate thumbnail left (counter clockwise) 90 degrees.
  1783. The result of the rotation is displayed in the image display area
  1784. and a confirmation is needed before the original image files is
  1785. overwritten. This confirmation can be turned off using
  1786. `image-dired-rotate-original-ask-before-overwrite'."
  1787. (interactive)
  1788. (image-dired-rotate-thumbnail "270"))
  1789. (defun image-dired-rotate-thumbnail-right ()
  1790. "Rotate thumbnail counter right (clockwise) 90 degrees.
  1791. The result of the rotation is displayed in the image display area
  1792. and a confirmation is needed before the original image files is
  1793. overwritten. This confirmation can be turned off using
  1794. `image-dired-rotate-original-ask-before-overwrite'."
  1795. (interactive)
  1796. (image-dired-rotate-thumbnail "90"))
  1797. (defun image-dired-refresh-thumb ()
  1798. "Force creation of new image for current thumbnail."
  1799. (interactive)
  1800. (let* ((file (image-dired-original-file-name))
  1801. (thumb (expand-file-name (image-dired-thumb-name file))))
  1802. (clear-image-cache (expand-file-name thumb))
  1803. (image-dired-create-thumb file thumb)))
  1804. (defun image-dired-rotate-original (degrees)
  1805. "Rotate original image DEGREES degrees."
  1806. (image-dired--check-executable-exists
  1807. 'image-dired-cmd-rotate-original-program)
  1808. (if (not (image-dired-image-at-point-p))
  1809. (message "No image at point")
  1810. (let* ((file (image-dired-original-file-name))
  1811. (spec
  1812. (list
  1813. (cons ?d degrees)
  1814. (cons ?o (expand-file-name file))
  1815. (cons ?t image-dired-temp-rotate-image-file))))
  1816. (unless (eq 'jpeg (image-type file))
  1817. (error "Only JPEG images can be rotated!"))
  1818. (if (not (= 0 (apply #'call-process image-dired-cmd-rotate-original-program
  1819. nil nil nil
  1820. (mapcar (lambda (arg) (format-spec arg spec))
  1821. image-dired-cmd-rotate-original-options))))
  1822. (error "Could not rotate image")
  1823. (image-dired-display-image image-dired-temp-rotate-image-file)
  1824. (if (or (and image-dired-rotate-original-ask-before-overwrite
  1825. (y-or-n-p
  1826. "Rotate to temp file OK. Overwrite original image? "))
  1827. (not image-dired-rotate-original-ask-before-overwrite))
  1828. (progn
  1829. (copy-file image-dired-temp-rotate-image-file file t)
  1830. (image-dired-refresh-thumb))
  1831. (image-dired-display-image file))))))
  1832. (defun image-dired-rotate-original-left ()
  1833. "Rotate original image left (counter clockwise) 90 degrees."
  1834. (interactive)
  1835. (image-dired-rotate-original "270"))
  1836. (defun image-dired-rotate-original-right ()
  1837. "Rotate original image right (clockwise) 90 degrees."
  1838. (interactive)
  1839. (image-dired-rotate-original "90"))
  1840. (defun image-dired-get-exif-file-name (file)
  1841. "Use the image's EXIF information to return a unique file name.
  1842. The file name should be unique as long as you do not take more than
  1843. one picture per second. The original file name is suffixed at the end
  1844. for traceability. The format of the returned file name is
  1845. YYYY_MM_DD_HH_MM_DD_ORIG_FILE_NAME.jpg. Used from
  1846. `image-dired-copy-with-exif-file-name'."
  1847. (let (data no-exif-data-found)
  1848. (if (not (eq 'jpeg (image-type (expand-file-name file))))
  1849. (setq no-exif-data-found t
  1850. data (format-time-string
  1851. "%Y:%m:%d %H:%M:%S"
  1852. (file-attribute-modification-time
  1853. (file-attributes (expand-file-name file)))))
  1854. (setq data (image-dired-get-exif-data (expand-file-name file)
  1855. "DateTimeOriginal")))
  1856. (while (string-match "[ :]" data)
  1857. (setq data (replace-match "_" nil nil data)))
  1858. (format "%s%s%s" data
  1859. (if no-exif-data-found
  1860. "_noexif_"
  1861. "_")
  1862. (file-name-nondirectory file))))
  1863. (defun image-dired-thumbnail-set-image-description ()
  1864. "Set the ImageDescription EXIF tag for the original image.
  1865. If the image already has a value for this tag, it is used as the
  1866. default value at the prompt."
  1867. (interactive)
  1868. (if (not (image-dired-image-at-point-p))
  1869. (message "No thumbnail at point")
  1870. (let* ((file (image-dired-original-file-name))
  1871. (old-value (image-dired-get-exif-data file "ImageDescription")))
  1872. (if (eq 0
  1873. (image-dired-set-exif-data file "ImageDescription"
  1874. (read-string "Value of ImageDescription: "
  1875. old-value)))
  1876. (message "Successfully wrote ImageDescription tag.")
  1877. (error "Could not write ImageDescription tag")))))
  1878. (defun image-dired-set-exif-data (file tag-name tag-value)
  1879. "In FILE, set EXIF tag TAG-NAME to value TAG-VALUE."
  1880. (image-dired--check-executable-exists
  1881. 'image-dired-cmd-write-exif-data-program)
  1882. (let ((spec
  1883. (list
  1884. (cons ?f (expand-file-name file))
  1885. (cons ?t tag-name)
  1886. (cons ?v tag-value))))
  1887. (apply #'call-process image-dired-cmd-write-exif-data-program nil nil nil
  1888. (mapcar (lambda (arg) (format-spec arg spec))
  1889. image-dired-cmd-write-exif-data-options))))
  1890. (defun image-dired-get-exif-data (file tag-name)
  1891. "From FILE, return EXIF tag TAG-NAME."
  1892. (image-dired--check-executable-exists
  1893. 'image-dired-cmd-read-exif-data-program)
  1894. (let ((buf (get-buffer-create "*image-dired-get-exif-data*"))
  1895. (spec (list (cons ?f file) (cons ?t tag-name)))
  1896. tag-value)
  1897. (with-current-buffer buf
  1898. (delete-region (point-min) (point-max))
  1899. (if (not (eq (apply #'call-process image-dired-cmd-read-exif-data-program
  1900. nil t nil
  1901. (mapcar
  1902. (lambda (arg) (format-spec arg spec))
  1903. image-dired-cmd-read-exif-data-options))
  1904. 0))
  1905. (error "Could not get EXIF tag")
  1906. (goto-char (point-min))
  1907. ;; Clean buffer from newlines and carriage returns before
  1908. ;; getting final info
  1909. (while (search-forward-regexp "[\n\r]" nil t)
  1910. (replace-match "" nil t))
  1911. (setq tag-value (buffer-substring (point-min) (point-max)))))
  1912. tag-value))
  1913. (defun image-dired-copy-with-exif-file-name ()
  1914. "Copy file with unique name to main image directory.
  1915. Copy current or all marked files in dired to a new file in your
  1916. main image directory, using a file name generated by
  1917. `image-dired-get-exif-file-name'. A typical usage for this if when
  1918. copying images from a digital camera into the image directory.
  1919. Typically, you would open up the folder with the incoming
  1920. digital images, mark the files to be copied, and execute this
  1921. function. The result is a couple of new files in
  1922. `image-dired-main-image-directory' called
  1923. 2005_05_08_12_52_00_dscn0319.jpg,
  1924. 2005_05_08_14_27_45_dscn0320.jpg etc."
  1925. (interactive)
  1926. (let (new-name
  1927. (files (dired-get-marked-files)))
  1928. (mapc
  1929. (lambda (curr-file)
  1930. (setq new-name
  1931. (format "%s/%s"
  1932. (file-name-as-directory
  1933. (expand-file-name image-dired-main-image-directory))
  1934. (image-dired-get-exif-file-name curr-file)))
  1935. (message "Copying %s to %s" curr-file new-name)
  1936. (copy-file curr-file new-name))
  1937. files)))
  1938. (defun image-dired-display-next-thumbnail-original ()
  1939. "In thumbnail buffer, move to next thumbnail and display the image."
  1940. (interactive)
  1941. (image-dired-forward-image)
  1942. (image-dired-display-thumbnail-original-image))
  1943. (defun image-dired-display-previous-thumbnail-original ()
  1944. "Move to previous thumbnail and display image."
  1945. (interactive)
  1946. (image-dired-backward-image)
  1947. (image-dired-display-thumbnail-original-image))
  1948. (defun image-dired-write-comments (file-comments)
  1949. "Write file comments to database.
  1950. Write file comments to one or more files.
  1951. FILE-COMMENTS is an alist on the following form:
  1952. ((FILE . COMMENT) ... )"
  1953. (image-dired-sane-db-file)
  1954. (let (end comment-beg-pos comment-end-pos file comment)
  1955. (image-dired--with-db-file
  1956. (setq buffer-file-name image-dired-db-file)
  1957. (dolist (elt file-comments)
  1958. (setq file (car elt)
  1959. comment (cdr elt))
  1960. (goto-char (point-min))
  1961. (if (search-forward-regexp (format "^%s.*$" file) nil t)
  1962. (progn
  1963. (setq end (point))
  1964. (beginning-of-line)
  1965. ;; Delete old comment, if any
  1966. (when (search-forward ";comment:" end t)
  1967. (setq comment-beg-pos (match-beginning 0))
  1968. ;; Any tags after the comment?
  1969. (if (search-forward ";" end t)
  1970. (setq comment-end-pos (- (point) 1))
  1971. (setq comment-end-pos end))
  1972. ;; Delete comment tag and comment
  1973. (delete-region comment-beg-pos comment-end-pos))
  1974. ;; Insert new comment
  1975. (beginning-of-line)
  1976. (unless (search-forward ";" end t)
  1977. (end-of-line)
  1978. (insert ";"))
  1979. (insert (format "comment:%s;" comment)))
  1980. ;; File does not exist in database - add it.
  1981. (goto-char (point-max))
  1982. (insert (format "\n%s;comment:%s" file comment))))
  1983. (save-buffer))))
  1984. (defun image-dired-update-property (prop value)
  1985. "Update text property PROP with value VALUE at point."
  1986. (let ((inhibit-read-only t))
  1987. (put-text-property
  1988. (point) (1+ (point))
  1989. prop
  1990. value)))
  1991. ;;;###autoload
  1992. (defun image-dired-dired-comment-files ()
  1993. "Add comment to current or marked files in dired."
  1994. (interactive)
  1995. (let ((comment (image-dired-read-comment)))
  1996. (image-dired-write-comments
  1997. (mapcar
  1998. (lambda (curr-file)
  1999. (cons curr-file comment))
  2000. (dired-get-marked-files)))))
  2001. (defun image-dired-comment-thumbnail ()
  2002. "Add comment to current thumbnail in thumbnail buffer."
  2003. (interactive)
  2004. (let* ((file (image-dired-original-file-name))
  2005. (comment (image-dired-read-comment file)))
  2006. (image-dired-write-comments (list (cons file comment)))
  2007. (image-dired-update-property 'comment comment))
  2008. (image-dired-display-thumb-properties))
  2009. (defun image-dired-read-comment (&optional file)
  2010. "Read comment for an image.
  2011. Optionally use old comment from FILE as initial value."
  2012. (let ((comment
  2013. (read-string
  2014. "Comment: "
  2015. (if file (image-dired-get-comment file)))))
  2016. comment))
  2017. (defun image-dired-get-comment (file)
  2018. "Get comment for file FILE."
  2019. (image-dired-sane-db-file)
  2020. (image-dired--with-db-file
  2021. (let (end comment-beg-pos comment-end-pos comment)
  2022. (when (search-forward-regexp (format "^%s" file) nil t)
  2023. (end-of-line)
  2024. (setq end (point))
  2025. (beginning-of-line)
  2026. (when (search-forward ";comment:" end t)
  2027. (setq comment-beg-pos (point))
  2028. (if (search-forward ";" end t)
  2029. (setq comment-end-pos (- (point) 1))
  2030. (setq comment-end-pos end))
  2031. (setq comment (buffer-substring
  2032. comment-beg-pos comment-end-pos))))
  2033. comment)))
  2034. ;;;###autoload
  2035. (defun image-dired-mark-tagged-files ()
  2036. "Use regexp to mark files with matching tag.
  2037. A `tag' is a keyword, a piece of meta data, associated with an
  2038. image file and stored in image-dired's database file. This command
  2039. lets you input a regexp and this will be matched against all tags
  2040. on all image files in the database file. The files that have a
  2041. matching tag will be marked in the dired buffer."
  2042. (interactive)
  2043. (image-dired-sane-db-file)
  2044. (let ((tag (read-string "Mark tagged files (regexp): "))
  2045. (hits 0)
  2046. files)
  2047. (image-dired--with-db-file
  2048. ;; Collect matches
  2049. (while (search-forward-regexp
  2050. (concat "\\(^[^;\n]+\\);.*" tag ".*$") nil t)
  2051. (push (match-string 1) files)))
  2052. ;; Mark files
  2053. (dolist (curr-file files)
  2054. ;; I tried using `dired-mark-files-regexp' but it was waaaay to
  2055. ;; slow. Don't bother about hits found in other directories
  2056. ;; than the current one.
  2057. (when (string= (file-name-as-directory
  2058. (expand-file-name default-directory))
  2059. (file-name-as-directory
  2060. (file-name-directory curr-file)))
  2061. (setq curr-file (file-name-nondirectory curr-file))
  2062. (goto-char (point-min))
  2063. (when (search-forward-regexp (format "\\s %s$" curr-file) nil t)
  2064. (setq hits (+ hits 1))
  2065. (dired-mark 1))))
  2066. (message "%d files with matching tag marked." hits)))
  2067. (defun image-dired-mouse-display-image (event)
  2068. "Use mouse EVENT, call `image-dired-display-image' to display image.
  2069. Track this in associated dired buffer if `image-dired-track-movement' is
  2070. non-nil."
  2071. (interactive "e")
  2072. (mouse-set-point event)
  2073. (goto-char (posn-point (event-end event)))
  2074. (let ((file (image-dired-original-file-name)))
  2075. (when file
  2076. (if image-dired-track-movement
  2077. (image-dired-track-original-file))
  2078. (image-dired-create-display-image-buffer)
  2079. (display-buffer image-dired-display-image-buffer)
  2080. (image-dired-display-image file))))
  2081. (defun image-dired-mouse-select-thumbnail (event)
  2082. "Use mouse EVENT to select thumbnail image.
  2083. Track this in associated dired buffer if `image-dired-track-movement' is
  2084. non-nil."
  2085. (interactive "e")
  2086. (mouse-set-point event)
  2087. (goto-char (posn-point (event-end event)))
  2088. (if image-dired-track-movement
  2089. (image-dired-track-original-file))
  2090. (image-dired-display-thumb-properties))
  2091. (defun image-dired-mouse-toggle-mark (event)
  2092. "Use mouse EVENT to toggle dired mark for thumbnail.
  2093. Track this in associated dired buffer if `image-dired-track-movement' is
  2094. non-nil."
  2095. (interactive "e")
  2096. (mouse-set-point event)
  2097. (goto-char (posn-point (event-end event)))
  2098. (if image-dired-track-movement
  2099. (image-dired-track-original-file))
  2100. (image-dired-toggle-mark-thumb-original-file))
  2101. (defun image-dired-dired-display-properties ()
  2102. "Display properties for dired file in the echo area."
  2103. (interactive)
  2104. (let* ((file (dired-get-filename))
  2105. (file-name (file-name-nondirectory file))
  2106. (dired-buf (buffer-name (current-buffer)))
  2107. (props (mapconcat #'identity (image-dired-list-tags file) ", "))
  2108. (comment (image-dired-get-comment file))
  2109. (message-log-max nil))
  2110. (if file-name
  2111. (message "%s"
  2112. (image-dired-format-properties-string
  2113. dired-buf
  2114. file-name
  2115. props
  2116. comment)))))
  2117. (defvar image-dired-tag-file-list nil
  2118. "List to store tag-file structure.")
  2119. (defvar image-dired-file-tag-list nil
  2120. "List to store file-tag structure.")
  2121. (defvar image-dired-file-comment-list nil
  2122. "List to store file comments.")
  2123. (defun image-dired-add-to-tag-file-list (tag file)
  2124. "Add relation between TAG and FILE."
  2125. (let (curr)
  2126. (if image-dired-tag-file-list
  2127. (if (setq curr (assoc tag image-dired-tag-file-list))
  2128. (if (not (member file curr))
  2129. (setcdr curr (cons file (cdr curr))))
  2130. (setcdr image-dired-tag-file-list
  2131. (cons (list tag file) (cdr image-dired-tag-file-list))))
  2132. (setq image-dired-tag-file-list (list (list tag file))))))
  2133. (defun image-dired-add-to-tag-file-lists (tag file)
  2134. "Helper function used from `image-dired-create-gallery-lists'.
  2135. Add TAG to FILE in one list and FILE to TAG in the other.
  2136. Lisp structures look like the following:
  2137. image-dired-file-tag-list:
  2138. ((\"filename1\" \"tag1\" \"tag2\" \"tag3\" ...)
  2139. (\"filename2\" \"tag1\" \"tag2\" \"tag3\" ...)
  2140. ...)
  2141. image-dired-tag-file-list:
  2142. ((\"tag1\" \"filename1\" \"filename2\" \"filename3\" ...)
  2143. (\"tag2\" \"filename1\" \"filename2\" \"filename3\" ...)
  2144. ...)"
  2145. ;; Add tag to file list
  2146. (let (curr)
  2147. (if image-dired-file-tag-list
  2148. (if (setq curr (assoc file image-dired-file-tag-list))
  2149. (setcdr curr (cons tag (cdr curr)))
  2150. (setcdr image-dired-file-tag-list
  2151. (cons (list file tag) (cdr image-dired-file-tag-list))))
  2152. (setq image-dired-file-tag-list (list (list file tag))))
  2153. ;; Add file to tag list
  2154. (if image-dired-tag-file-list
  2155. (if (setq curr (assoc tag image-dired-tag-file-list))
  2156. (if (not (member file curr))
  2157. (setcdr curr (cons file (cdr curr))))
  2158. (setcdr image-dired-tag-file-list
  2159. (cons (list tag file) (cdr image-dired-tag-file-list))))
  2160. (setq image-dired-tag-file-list (list (list tag file))))))
  2161. (defun image-dired-add-to-file-comment-list (file comment)
  2162. "Helper function used from `image-dired-create-gallery-lists'.
  2163. For FILE, add COMMENT to list.
  2164. Lisp structure looks like the following:
  2165. image-dired-file-comment-list:
  2166. ((\"filename1\" . \"comment1\")
  2167. (\"filename2\" . \"comment2\")
  2168. ...)"
  2169. (if image-dired-file-comment-list
  2170. (if (not (assoc file image-dired-file-comment-list))
  2171. (setcdr image-dired-file-comment-list
  2172. (cons (cons file comment)
  2173. (cdr image-dired-file-comment-list))))
  2174. (setq image-dired-file-comment-list (list (cons file comment)))))
  2175. (defun image-dired-create-gallery-lists ()
  2176. "Create temporary lists used by `image-dired-gallery-generate'."
  2177. (image-dired-sane-db-file)
  2178. (image-dired--with-db-file
  2179. (let (end beg file row-tags)
  2180. (setq image-dired-tag-file-list nil)
  2181. (setq image-dired-file-tag-list nil)
  2182. (setq image-dired-file-comment-list nil)
  2183. (goto-char (point-min))
  2184. (while (search-forward-regexp "^." nil t)
  2185. (end-of-line)
  2186. (setq end (point))
  2187. (beginning-of-line)
  2188. (setq beg (point))
  2189. (unless (search-forward ";" end nil)
  2190. (error "Something is really wrong, check format of database"))
  2191. (setq row-tags (split-string
  2192. (buffer-substring beg end) ";"))
  2193. (setq file (car row-tags))
  2194. (dolist (x (cdr row-tags))
  2195. (if (not (string-match "^comment:\\(.*\\)" x))
  2196. (image-dired-add-to-tag-file-lists x file)
  2197. (image-dired-add-to-file-comment-list file (match-string 1 x)))))))
  2198. ;; Sort tag-file list
  2199. (setq image-dired-tag-file-list
  2200. (sort image-dired-tag-file-list
  2201. (lambda (x y)
  2202. (string< (car x) (car y))))))
  2203. (defun image-dired-hidden-p (file)
  2204. "Return t if image FILE has a \"hidden\" tag."
  2205. (cl-loop for tag in (cdr (assoc file image-dired-file-tag-list))
  2206. if (member tag image-dired-gallery-hidden-tags) return t))
  2207. (defun image-dired-gallery-generate ()
  2208. "Generate gallery pages.
  2209. First we create a couple of Lisp structures from the database to make
  2210. it easier to generate, then HTML-files are created in
  2211. `image-dired-gallery-dir'."
  2212. (interactive)
  2213. (if (eq 'per-directory image-dired-thumbnail-storage)
  2214. (error "Currently, gallery generation is not supported \
  2215. when using per-directory thumbnail file storage"))
  2216. (image-dired-create-gallery-lists)
  2217. (let ((tags image-dired-tag-file-list)
  2218. (index-file (format "%s/index.html" image-dired-gallery-dir))
  2219. count tag tag-file
  2220. comment file-tags tag-link tag-link-list)
  2221. ;; Make sure gallery root exist
  2222. (if (file-exists-p image-dired-gallery-dir)
  2223. (if (not (file-directory-p image-dired-gallery-dir))
  2224. (error "Variable image-dired-gallery-dir is not a directory"))
  2225. (make-directory image-dired-gallery-dir))
  2226. ;; Open index file
  2227. (with-temp-file index-file
  2228. (if (file-exists-p index-file)
  2229. (insert-file-contents index-file))
  2230. (insert "<html>\n")
  2231. (insert " <body>\n")
  2232. (insert " <h2>Image-Dired Gallery</h2>\n")
  2233. (insert (format "<p>\n Gallery generated %s\n <p>\n"
  2234. (current-time-string)))
  2235. (insert " <h3>Tag index</h3>\n")
  2236. (setq count 1)
  2237. ;; Pre-generate list of all tag links
  2238. (dolist (curr tags)
  2239. (setq tag (car curr))
  2240. (when (not (member tag image-dired-gallery-hidden-tags))
  2241. (setq tag-link (format "<a href=\"%d.html\">%s</a>" count tag))
  2242. (if tag-link-list
  2243. (setq tag-link-list
  2244. (append tag-link-list (list (cons tag tag-link))))
  2245. (setq tag-link-list (list (cons tag tag-link))))
  2246. (setq count (1+ count))))
  2247. (setq count 1)
  2248. ;; Main loop where we generated thumbnail pages per tag
  2249. (dolist (curr tags)
  2250. (setq tag (car curr))
  2251. ;; Don't display hidden tags
  2252. (when (not (member tag image-dired-gallery-hidden-tags))
  2253. ;; Insert link to tag page in index
  2254. (insert (format " %s<br>\n" (cdr (assoc tag tag-link-list))))
  2255. ;; Open per-tag file
  2256. (setq tag-file (format "%s/%s.html" image-dired-gallery-dir count))
  2257. (with-temp-file tag-file
  2258. (if (file-exists-p tag-file)
  2259. (insert-file-contents tag-file))
  2260. (erase-buffer)
  2261. (insert "<html>\n")
  2262. (insert " <body>\n")
  2263. (insert " <p><a href=\"index.html\">Index</a></p>\n")
  2264. (insert (format " <h2>Images with tag &quot;%s&quot;</h2>" tag))
  2265. ;; Main loop for files per tag page
  2266. (dolist (file (cdr curr))
  2267. (unless (image-dired-hidden-p file)
  2268. ;; Insert thumbnail with link to full image
  2269. (insert
  2270. (format "<a href=\"%s/%s\"><img src=\"%s/%s\"%s></a>\n"
  2271. image-dired-gallery-image-root-url
  2272. (file-name-nondirectory file)
  2273. image-dired-gallery-thumb-image-root-url
  2274. (file-name-nondirectory (image-dired-thumb-name file)) file))
  2275. ;; Insert comment, if any
  2276. (if (setq comment (cdr (assoc file image-dired-file-comment-list)))
  2277. (insert (format "<br>\n%s<br>\n" comment))
  2278. (insert "<br>\n"))
  2279. ;; Insert links to other tags, if any
  2280. (when (> (length
  2281. (setq file-tags (assoc file image-dired-file-tag-list))) 2)
  2282. (insert "[ ")
  2283. (dolist (extra-tag file-tags)
  2284. ;; Only insert if not file name or the main tag
  2285. (if (and (not (equal extra-tag tag))
  2286. (not (equal extra-tag file)))
  2287. (insert
  2288. (format "%s " (cdr (assoc extra-tag tag-link-list))))))
  2289. (insert "]<br>\n"))))
  2290. (insert " <p><a href=\"index.html\">Index</a></p>\n")
  2291. (insert " </body>\n")
  2292. (insert "</html>\n"))
  2293. (setq count (1+ count))))
  2294. (insert " </body>\n")
  2295. (insert "</html>"))))
  2296. (defvar image-dired-widget-list nil
  2297. "List to keep track of meta data in edit buffer.")
  2298. (declare-function widget-forward "wid-edit" (arg))
  2299. ;;;###autoload
  2300. (defun image-dired-dired-edit-comment-and-tags ()
  2301. "Edit comment and tags of current or marked image files.
  2302. Edit comment and tags for all marked image files in an
  2303. easy-to-use form."
  2304. (interactive)
  2305. (setq image-dired-widget-list nil)
  2306. ;; Setup buffer.
  2307. (let ((files (dired-get-marked-files)))
  2308. (pop-to-buffer-same-window "*Image-Dired Edit Meta Data*")
  2309. (kill-all-local-variables)
  2310. (make-local-variable 'widget-example-repeat)
  2311. (let ((inhibit-read-only t))
  2312. (erase-buffer))
  2313. (remove-overlays)
  2314. ;; Some help for the user.
  2315. (widget-insert
  2316. "\nEdit comments and tags for each image. Separate multiple tags
  2317. with a comma. Move forward between fields using TAB or RET.
  2318. Move to the previous field using backtab (S-TAB). Save by
  2319. activating the Save button at the bottom of the form or cancel
  2320. the operation by activating the Cancel button.\n\n")
  2321. ;; Here comes all images and a comment and tag field for each
  2322. ;; image.
  2323. (let (thumb-file img comment-widget tag-widget)
  2324. (dolist (file files)
  2325. (setq thumb-file (image-dired-thumb-name file)
  2326. img (create-image thumb-file))
  2327. (insert-image img)
  2328. (widget-insert "\n\nComment: ")
  2329. (setq comment-widget
  2330. (widget-create 'editable-field
  2331. :size 60
  2332. :format "%v "
  2333. :value (or (image-dired-get-comment file) "")))
  2334. (widget-insert "\nTags: ")
  2335. (setq tag-widget
  2336. (widget-create 'editable-field
  2337. :size 60
  2338. :format "%v "
  2339. :value (or (mapconcat
  2340. #'identity
  2341. (image-dired-list-tags file)
  2342. ",") "")))
  2343. ;; Save information in all widgets so that we can use it when
  2344. ;; the user saves the form.
  2345. (setq image-dired-widget-list
  2346. (append image-dired-widget-list
  2347. (list (list file comment-widget tag-widget))))
  2348. (widget-insert "\n\n")))
  2349. ;; Footer with Save and Cancel button.
  2350. (widget-insert "\n")
  2351. (widget-create 'push-button
  2352. :notify
  2353. (lambda (&rest _ignore)
  2354. (image-dired-save-information-from-widgets)
  2355. (bury-buffer)
  2356. (message "Done."))
  2357. "Save")
  2358. (widget-insert " ")
  2359. (widget-create 'push-button
  2360. :notify
  2361. (lambda (&rest _ignore)
  2362. (bury-buffer)
  2363. (message "Operation canceled."))
  2364. "Cancel")
  2365. (widget-insert "\n")
  2366. (use-local-map widget-keymap)
  2367. (widget-setup)
  2368. ;; Jump to the first widget.
  2369. (widget-forward 1)))
  2370. (defun image-dired-save-information-from-widgets ()
  2371. "Save information found in `image-dired-widget-list'.
  2372. Use the information in `image-dired-widget-list' to save comments and
  2373. tags to their respective image file. Internal function used by
  2374. `image-dired-dired-edit-comment-and-tags'."
  2375. (let (file comment tag-string tag-list lst)
  2376. (image-dired-write-comments
  2377. (mapcar
  2378. (lambda (widget)
  2379. (setq file (car widget)
  2380. comment (widget-value (cadr widget)))
  2381. (cons file comment))
  2382. image-dired-widget-list))
  2383. (image-dired-write-tags
  2384. (dolist (widget image-dired-widget-list lst)
  2385. (setq file (car widget)
  2386. tag-string (widget-value (car (cddr widget)))
  2387. tag-list (split-string tag-string ","))
  2388. (dolist (tag tag-list)
  2389. (push (cons file tag) lst))))))
  2390. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2391. ;;;;;;;;; TEST-SECTION ;;;;;;;;;;;
  2392. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2393. ;; (defvar image-dired-dir-max-size 12300000)
  2394. ;; (defun image-dired-test-clean-old-files ()
  2395. ;; "Clean `image-dired-dir' from old thumbnail files.
  2396. ;; \"Oldness\" measured using last access time. If the total size of all
  2397. ;; thumbnail files in `image-dired-dir' is larger than 'image-dired-dir-max-size',
  2398. ;; old files are deleted until the max size is reached."
  2399. ;; (let* ((files
  2400. ;; (sort
  2401. ;; (mapcar
  2402. ;; (lambda (f)
  2403. ;; (let ((fattribs (file-attributes f)))
  2404. ;; ;; Get last access time and file size
  2405. ;; `(,(nth 4 fattribs) ,(nth 7 fattribs) ,f)))
  2406. ;; (directory-files (image-dired-dir) t ".+\\.thumb\\..+$"))
  2407. ;; ;; Sort function. Compare time between two files.
  2408. ;; (lambda (l1 l2)
  2409. ;; (time-less-p (car l1) (car l2)))))
  2410. ;; (dirsize (apply '+ (mapcar (lambda (x) (cadr x)) files))))
  2411. ;; (while (> dirsize image-dired-dir-max-size)
  2412. ;; (y-or-n-p
  2413. ;; (format "Size of thumbnail directory: %d, delete old file %s? "
  2414. ;; dirsize (cadr (cdar files))))
  2415. ;; (delete-file (cadr (cdar files)))
  2416. ;; (setq dirsize (- dirsize (car (cdar files))))
  2417. ;; (setq files (cdr files)))))
  2418. ;;;;;;;;;;;;;;;;;;;;;;,
  2419. ;; (defun dired-speedbar-buttons (dired-buffer)
  2420. ;; (when (and (boundp 'image-dired-use-speedbar)
  2421. ;; image-dired-use-speedbar)
  2422. ;; (let ((filename (with-current-buffer dired-buffer
  2423. ;; (dired-get-filename))))
  2424. ;; (when (and (not (string-equal filename (buffer-string)))
  2425. ;; (string-match (image-file-name-regexp) filename))
  2426. ;; (erase-buffer)
  2427. ;; (insert (propertize
  2428. ;; filename
  2429. ;; 'display
  2430. ;; (image-dired-get-thumbnail-image filename)))))))
  2431. ;; (setq image-dired-use-speedbar t)
  2432. (provide 'image-dired)
  2433. ;;; image-dired.el ends here