Skip to content

Commit 465407d

Browse files
committed
feat(attach): add org-attach feature
1 parent 6db042e commit 465407d

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]]
@@ -1154,6 +1155,171 @@ Using the example above, setting this variable to ={'MYTAG'}=, second
11541155
and third headline would have only =CHILDTAG=, where =MYTAG= would not
11551156
be inherited.
11561157

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

2233+
**** =org_attach=
2234+
:PROPERTIES:
2235+
:CUSTOM_ID: org_attach
2236+
:END:
2237+
- Mapped to: =<Leader>o<C-A>=
2238+
Open the attach dispatcher. See [[#attachments][Attachments]] for more details.
2239+
20672240
**** =org_show_help=
20682241
:PROPERTIES:
20692242
:CUSTOM_ID: org_show_help
@@ -2748,6 +2921,43 @@ Running [[#org_babel_tangle][org_babel_tangle]] will create file =~/org/my_tangl
27482921
=print('Headline 1')=
27492922
=#+end_src=
27502923

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

0 commit comments

Comments
 (0)