Skip to content

Commit 4f39de3

Browse files
committed
feat(attach): add org-attach feature
1 parent d771717 commit 4f39de3

File tree

13 files changed

+2883
-0
lines changed

13 files changed

+2883
-0
lines changed

docs/configuration.org

Lines changed: 210 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]]
@@ -1159,6 +1160,171 @@ Using the example above, setting this variable to ={'MYTAG'}=, second
11591160
and third headline would have only =CHILDTAG=, where =MYTAG= would not
11601161
be inherited.
11611162

1163+
** Attachments settings
1164+
:PROPERTIES:
1165+
:CUSTOM_ID: attachment-settings
1166+
:END:
1167+
1168+
*** =org_attach_id_dir=
1169+
:PROPERTIES:
1170+
:CUSTOM_ID: org_attach_id_dir
1171+
:END:
1172+
- Type: =string=
1173+
- Default: ='./data/'=
1174+
1175+
The directory where attachments are stored. If this is a relative path, it
1176+
will be interpreted relative to the directory where the Org file lives.
1177+
1178+
*** =org_attach_dir_relative=
1179+
:PROPERTIES:
1180+
:CUSTOM_ID: org_attach_dir_relative
1181+
:END:
1182+
- Type: =boolean=
1183+
- Default: =false=
1184+
1185+
If =true=, whenever you add a =DIR= property to a headline, it is added as
1186+
a relative path. The default is to only add absolute paths.
1187+
1188+
*** =org_attach_auto_tag=
1189+
:PROPERTIES:
1190+
:CUSTOM_ID: org_attach_auto_tag
1191+
:END:
1192+
- Type: =string=
1193+
- Default: ='ATTACH'=
1194+
1195+
Tag that is added automatically when attaching files to a headline.
1196+
1197+
*** =org_attach_preferred_new_method=
1198+
:PROPERTIES:
1199+
:CUSTOM_ID: org_attach_preferred_new_method
1200+
:END:
1201+
- Type: ='id'|'dir'|'ask'|false=
1202+
- Default: ='id'=
1203+
1204+
This setting is used when attaching files to nodes that have neither an
1205+
=ID= nor a =DIR= property.
1206+
1207+
- =id= - create and use an =ID= property
1208+
- =dir= - create and use a =DIR= property
1209+
- =ask= - ask the user which method to use
1210+
- =false= - don't create a property; the user has to define it explicitly before attaching files
1211+
1212+
*** =org_attach_method=
1213+
:PROPERTIES:
1214+
:CUSTOM_ID: org_attach_method
1215+
:END:
1216+
- Type: ='cp'|'mv'|'ln'|'lns'=
1217+
- Default: ='cp'=
1218+
1219+
The preferred method to add files to the attachment directory.
1220+
1221+
- =mv= - move (rename) the file
1222+
- =cp= - copy the file
1223+
- =ln= - create a hard link; not supported on all systems
1224+
- =lns= - create a symbol link; not supported on all systems; on Windows, this always creates a /junction/
1225+
1226+
*** =org_attach_copy_directory_create_symlink=
1227+
:PROPERTIES:
1228+
:CUSTOM_ID: org_attach_copy_directory_create_symlink
1229+
:END:
1230+
- Type: =boolean=
1231+
- Default: =false=
1232+
1233+
If =true=, whenever the attachments directory itself is a symlink, and it
1234+
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
1235+
symlink itself. The default is to treat the symlink transparently as
1236+
a directory.
1237+
1238+
*** =org_attach_visit_command=
1239+
:PROPERTIES:
1240+
:CUSTOM_ID: org_attach_visit_command
1241+
:END:
1242+
- Type: =string|fun(path: string)=
1243+
- Default: ='edit'=
1244+
1245+
Command or function used to open a directory. The default opens NetRW if it
1246+
is available.
1247+
1248+
*** =org_attach_use_inheritance=
1249+
:PROPERTIES:
1250+
:CUSTOM_ID: org_attach_use_inheritance
1251+
:END:
1252+
- Type: ='always'|'selective'|'never'=
1253+
- Default: ='selective'=
1254+
1255+
Attachment inheritance for the outline.
1256+
1257+
Enabling inheritance implies two things:
1258+
1. Attachment links will look through all parent headlines until they find
1259+
the linked attachment.
1260+
2. Running =attach= inside a node without attachments will operate on the
1261+
first parent headline that has an attachment.
1262+
1263+
Possible values are:
1264+
1265+
- =always= - inherit attachments
1266+
- =selective= - respect [[#org_use_property_inheritance][org_use_property_inheritance]] for the properties =DIR= and =ID=
1267+
- =never= - don't inherit attachments
1268+
1269+
*** =org_attach_store_link_p=
1270+
:PROPERTIES:
1271+
:CUSTOM_ID: org_attach_store_link_p
1272+
:END:
1273+
- Type: ='attached' | 'file' | 'original' | false=
1274+
- Default: ='attached'=
1275+
1276+
If not =false=, store a link with [[#org_store_link][org_store_link]] when attaching a file.
1277+
1278+
- =attach= - store a =[[attachment:name]]= link
1279+
- =file= - store a =[[file:attach_dir/name]]= link
1280+
- =original= - store a =[[file:original/location]]= link
1281+
1282+
*** =org_attach_archive_delete=
1283+
:PROPERTIES:
1284+
:CUSTOM_ID: org_attach_archive_delete
1285+
:END:
1286+
- Type: ='always' | 'ask' | 'never'=
1287+
- Default: ='never'=
1288+
1289+
Determines whether attachments are deleted automatically whenever a subtree
1290+
is moved to an archive file. The value ='ask'= means to ask the user.
1291+
1292+
*** =org_attach_id_to_path_function_list=
1293+
:PROPERTIES:
1294+
:CUSTOM_ID: org_attach_id_to_path_function_list
1295+
:END:
1296+
- Type: =(string | fun(id: string): (string|nil))[]=
1297+
- Default: ={ 'uuid_folder_format', 'ts_folder_format', 'fallback_folder_format' }=
1298+
1299+
List of functions that are tried sequentially to derive an attachment path
1300+
from an =ID= property. The functions are called with a single =id= argument
1301+
until the return value is an existing folder. The ID format passed to the
1302+
functions is usually defined by [[#org_id_method][org_id_method]].
1303+
1304+
If no folder has been created yet for the given ID, then the first truthy
1305+
value defines the path of the folder to be created.
1306+
1307+
The default functions avoid putting all attachment directories directly
1308+
inside [[#org_attach_id_dir][org_attach_id_dir]]. Some file systems have performance issues in
1309+
such scenarios.
1310+
1311+
Be careful when changing this setting. If you remove a function, previously
1312+
created attachment folders may be no longer mapped correctly and Org may be
1313+
unable to detect them.
1314+
1315+
*** =org_attach_sync_delete_empty_dir=
1316+
:PROPERTIES:
1317+
:CUSTOM_ID: org_attach_sync_delete_empty_dir
1318+
:END:
1319+
- Type: ='always'|'ask'|'never'=
1320+
- Default: ='ask'=
1321+
1322+
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]].
1323+
1324+
- =never= - never delete empty directories
1325+
- =ask= - ask the user whether to delete
1326+
- =always= - delete empty directories without asking
1327+
11621328
** Mappings
11631329
:PROPERTIES:
11641330
:CUSTOM_ID: mappings
@@ -2069,6 +2235,13 @@ See [[#clocking][Clocking]] for more details.
20692235
- Mapped to: =<leader>obt=
20702236
Tangle current file. See [[#extract-source-code-tangle][Extract source code (tangle)]] for more details.
20712237

2238+
**** =org_attach=
2239+
:PROPERTIES:
2240+
:CUSTOM_ID: org_attach
2241+
:END:
2242+
- Mapped to: =<Leader>o<C-A>=
2243+
Open the attach dispatcher. See [[#attachments][Attachments]] for more details.
2244+
20722245
**** =org_show_help=
20732246
:PROPERTIES:
20742247
:CUSTOM_ID: org_show_help
@@ -2753,6 +2926,43 @@ Running [[#org_babel_tangle][org_babel_tangle]] will create file =~/org/my_tangl
27532926
=print('Headline 1')=
27542927
=#+end_src=
27552928

2929+
*** Attachments
2930+
:PROPERTIES:
2931+
:CUSTOM_ID: attachments
2932+
:END:
2933+
2934+
There is almost complete support for file attachments (Orgmode link:
2935+
[[https://orgmode.org/manual/Attachments.html][Attachments]]). You can use [[#org_attach][org_attach]] to open the dispatcher and attach
2936+
files to an "attachment node" (either a headline or an entire org
2937+
file).
2938+
2939+
Attaching a file puts it in a directory associated with the attachment node.
2940+
Based on [[#org_attach_preferred_new_method][org_attach_preferred_new_method]], this either uses the =ID= or
2941+
the =DIR= property. See also [[#org_attach_id_dir][org_attach_id_dir]],
2942+
[[#org_attach_dir_relative][org_attach_dir_relative]], [[#org_attach_id_to_path_function_list][org_attach_id_to_path_function_list]] and
2943+
[[#org_attach_use_inheritance][org_attach_use_inheritance]] on how to further customize the attachments
2944+
directory.
2945+
2946+
Attachment links are supported. A link like =[[attachment:file.txt]]=
2947+
looks up =file.txt= in the current node's attachments directory and opens
2948+
it. Attaching a file stores a link to the attachment. See
2949+
[[#org_attach_store_link_p][org_attach_store_link_p]] on how to configure this behavior.
2950+
2951+
You can also attach files from a different buffer. The following
2952+
mapping attaches the path under the cursor to the current headline of the
2953+
most recently open org file:
2954+
2955+
#+begin_src lua
2956+
vim.keymap.set('n', '<Leader>o+', function()
2957+
local file = vim.fn.expand('<cfile>')
2958+
local org = require('orgmode')
2959+
org.attach:attach_to_other_buffer(file)
2960+
end)
2961+
#+end_src
2962+
2963+
The only missing feature is expansion of attachment links before exporting
2964+
a file with [[#org_export][org_exporting]].
2965+
27562966
** User interface
27572967
:PROPERTIES:
27582968
:CUSTOM_ID: user-interface

0 commit comments

Comments
 (0)