image-dired.el 100 KB

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