Skip to content

Commit 5b8b86d

Browse files
committed
feat(attach): add org-attach feature
1 parent af90f8f commit 5b8b86d

File tree

12 files changed

+2681
-0
lines changed

12 files changed

+2681
-0
lines changed

docs/configuration.org

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This page contains information about all configuration that can be provided to t
77
- [[#agenda-settings][Agenda settings]]
88
- [[#calendar-settings][Calendar settings]]
99
- [[#tags-settings][Tags settings]]
10+
- [[#attachment-settings][Attachments settings]]
1011
- [[#mappings][Mappings]]
1112
- [[#features][Features]]
1213
- [[#user-interface][User interface]]
@@ -1122,6 +1123,161 @@ Using the example above, setting this variable to ={'MYTAG'}=, second
11221123
and third headline would have only =CHILDTAG=, where =MYTAG= would not
11231124
be inherited.
11241125

1126+
** Attachments settings
1127+
:PROPERTIES:
1128+
:CUSTOM_ID: attachment-settings
1129+
:END:
1130+
1131+
*** org_attach_id_dir
1132+
:PROPERTIES:
1133+
:CUSTOM_ID: org_attach_id_dir
1134+
:END:
1135+
- Type: =string=
1136+
- Default: ='./data/'=
1137+
1138+
The directory where attachments are stored. If this is a relative path, it
1139+
will be interpreted relative to the directory where the Org file lives.
1140+
1141+
*** org_attach_auto_tag
1142+
:PROPERTIES:
1143+
:CUSTOM_ID: org_attach_auto_tag
1144+
:END:
1145+
- Type: =string=
1146+
- Default: ='ATTACH'=
1147+
1148+
Tag that is added automatically when attaching files to a headline.
1149+
1150+
*** org_attach_preferred_new_method
1151+
:PROPERTIES:
1152+
:CUSTOM_ID: org_attach_preferred_new_method
1153+
:END:
1154+
- Type: ='id'|'dir'|'ask'|false=
1155+
- Default: ='id'=
1156+
1157+
This setting is used when attaching files to nodes that have neither an
1158+
=ID= nor a =DIR= property.
1159+
1160+
- =id= - create and use an =ID= property
1161+
- =dir= - create and use a =DIR= property
1162+
- =ask= - ask the user which method to use
1163+
- =false= - don't create a property; the user has to define it explicitly before attaching files
1164+
1165+
*** org_attach_method
1166+
:PROPERTIES:
1167+
:CUSTOM_ID: org_attach_method
1168+
:END:
1169+
- Type: ='cp'|'mv'|'ln'|'lns'=
1170+
- Default: ='cp'=
1171+
1172+
The preferred method to add files to the attachment directory.
1173+
1174+
- =mv= - move (rename) the file
1175+
- =cp= - copy the file
1176+
- =ln= - create a hard link; not supported on all systems
1177+
- =lns= - create a symbol link; not supported on all systems; on Windows, this always creates a /junction/
1178+
1179+
*** org_attach_copy_directory_create_symlink
1180+
:PROPERTIES:
1181+
:CUSTOM_ID: org_attach_copy_directory_create_symlink
1182+
:END:
1183+
- Type: =boolean=
1184+
- Default: =false=
1185+
1186+
If =true=, whenever the attachments directory itself is a symlink, and it
1187+
is copied due to the [[https://orgmode.org/manual/Attachment-defaults-and-dispatcher.html*index-C_002dc-C_002da-s][set_directory]] or [[https://orgmode.org/manual/Attachment-defaults-and-dispatcher.html*index-C_002dc-C_002da-S][unset_directory]] action, copy the
1188+
symlink itself. The default is to treat the symlink transparently as
1189+
a directory.
1190+
1191+
*** org_attach_visit_command
1192+
:PROPERTIES:
1193+
:CUSTOM_ID: org_attach_visit_command
1194+
:END:
1195+
- Type: =string|fun(path: string)=
1196+
- Default: ='edit'=
1197+
1198+
Command or function used to open a directory. The default opens NetRW if it
1199+
is available.
1200+
1201+
*** org_attach_use_inheritance
1202+
:PROPERTIES:
1203+
:CUSTOM_ID: org_attach_use_inheritance
1204+
:END:
1205+
- Type: ='always'|'selective'|'never'=
1206+
- Default: ='selective'=
1207+
1208+
Attachment inheritance for the outline.
1209+
1210+
Enabling inheritance implies two things:
1211+
1. Attachment links will look through all parent headlines until they find
1212+
the linked attachment.
1213+
2. Running =attach= inside a node without attachments will operate on the
1214+
first parent headline that has an attachment.
1215+
1216+
Possible values are:
1217+
1218+
- =always= - inherit attachments
1219+
- =selective= - respect [[#org_use_property_inheritance][org_use_property_inheritance]] for the properties =DIR= and =ID=
1220+
- =never= - don't inherit attachments
1221+
1222+
*** org_attach_store_link_p
1223+
:PROPERTIES:
1224+
:CUSTOM_ID: org_attach_store_link_p
1225+
:END:
1226+
- Type: ='attached' | 'file' | 'original' | false=
1227+
- Default: ='attached'=
1228+
1229+
If not =false=, store a link with [[#org_store_link][org_store_link]] when attaching a file.
1230+
1231+
- =attach= - store a =[[attachment:name]]= link
1232+
- =file= - store a =[[file:attach_dir/name]]= link
1233+
- =original= - store a =[[file:original/location]]= link
1234+
1235+
*** org_attach_archive_delete
1236+
:PROPERTIES:
1237+
:CUSTOM_ID: org_attach_archive_delete
1238+
:END:
1239+
- Type: ='always' | 'ask' | 'never'=
1240+
- Default: ='never'=
1241+
1242+
Determines whether attachments are deleted automatically whenever a subtree
1243+
is moved to an archive file. The value ='ask'= means to ask the user.
1244+
1245+
*** org_attach_id_to_path_function_list
1246+
:PROPERTIES:
1247+
:CUSTOM_ID: org_attach_id_to_path_function_list
1248+
:END:
1249+
- Type: =(string | fun(id: string): (string|nil))[]=
1250+
- Default: ={ 'uuid_folder_format', 'ts_folder_format', 'fallback_folder_format' }=
1251+
1252+
List of functions that are tried sequentially to derive an attachment path
1253+
from an =ID= property. The functions are called with a single =id= argument
1254+
until the return value is an existing folder. The ID format passed to the
1255+
functions is usually defined by [[#org_id_method][org_id_method]].
1256+
1257+
If no folder has been created yet for the given ID, then the first truthy
1258+
value defines the path of the folder to be created.
1259+
1260+
The default functions avoid putting all attachment directories directly
1261+
inside [[#org_attach_id_dir][org_attach_id_dir]]. Some file systems have performance issues in
1262+
such scenarios.
1263+
1264+
Be careful when changing this setting. If you remove a function, previously
1265+
created attachment folders may be no longer mapped correctly and Org may be
1266+
unable to detect them.
1267+
1268+
*** org_attach_sync_delete_empty_dir
1269+
:PROPERTIES:
1270+
:CUSTOM_ID: org_attach_sync_delete_empty_dir
1271+
:END:
1272+
- Type: ='always'|'ask'|'never'=
1273+
- Default: ='ask'=
1274+
1275+
Determines whether to delete empty directories during [[https://orgmode.org/manual/Attachment-defaults-and-dispatcher.html*index-C_002dc-C_002da-z][org_attach_sync]].
1276+
1277+
- =never= - never delete empty directories
1278+
- =ask= - ask the user whether to delete
1279+
- =always= - delete empty directories without asking
1280+
11251281
** Mappings
11261282
:PROPERTIES:
11271283
:CUSTOM_ID: mappings
@@ -2032,6 +2188,13 @@ See [[#clocking][Clocking]] for more details.
20322188
- Mapped to: =<leader>obt=
20332189
Tangle current file. See [[#extract-source-code-tangle][Extract source code (tangle)]] for more details.
20342190

2191+
**** org_attach
2192+
:PROPERTIES:
2193+
:CUSTOM_ID: org_attach
2194+
:END:
2195+
- Mapped to: =<Leader>o<C-A>=
2196+
Open the attach dispatcher. See [[#attachments][Attachments]] for more details.
2197+
20352198
**** org_show_help
20362199
:PROPERTIES:
20372200
:CUSTOM_ID: org_show_help
@@ -2790,6 +2953,42 @@ Running [[#org_babel_tangle][org_babel_tangle]] will create file =~/org/my_tangl
27902953
=print('Headline 1')=
27912954
=#+end_src=
27922955

2956+
*** Attachments
2957+
:PROPERTIES:
2958+
:CUSTOM_ID: attachments
2959+
:END:
2960+
2961+
There is almost complete support for file attachments (Orgmode link:
2962+
[[https://orgmode.org/manual/Attachments.html][Attachments]]). You can use [[#org_attach][org_attach]] to open the dispatcher and attach
2963+
files to an "attachment node" (either a headline or an entire org
2964+
file).
2965+
2966+
Attaching a file puts it in a directory associated with the attachment node.
2967+
Based on [[#org_attach_preferred_new_method][org_attach_preferred_new_method]], this either uses the =ID= or
2968+
the =DIR= property. See also [[#org_attach_id_dir][org_attach_id_dir]],
2969+
[[#org_attach_id_to_path_function_list][org_attach_id_to_path_function_list]] and [[#org_attach_use_inheritance][org_attach_use_inheritance]] on how
2970+
to further customize the attachments directory.
2971+
2972+
Attachment links are supported. A link like =[[attachment:file.txt]]=
2973+
looks up =file.txt= in the current node's attachments directory and opens
2974+
it. Attaching a file stores a link to the attachment. See
2975+
[[#org_attach_store_link_p][org_attach_store_link_p]] on how to configure this behavior.
2976+
2977+
You can also attach files from a different buffer. The following
2978+
mapping attaches the path under the cursor to the current headline of the
2979+
most recently open org file:
2980+
2981+
#+begin_src lua
2982+
vim.keymap.set('n', '<Leader>o+', function()
2983+
local file = vim.fn.expand('<cfile>')
2984+
local org = require('orgmode')
2985+
org.attach:attach_to_other_buffer(file)
2986+
end)
2987+
#+end_src
2988+
2989+
The only missing feature is expansion of attachment links before exporting
2990+
a file with [[#org_export][org_exporting]].
2991+
27932992
** User interface
27942993
:PROPERTIES:
27952994
:CUSTOM_ID: user-interface

0 commit comments

Comments
 (0)