Skip to content

Commit 19c9367

Browse files
committed
feat(attach): add org-attach feature
The implementation follows the Emacs implementation in spirit, with some additional separation of concerns: 1. the actual logic is implemented in `attach/core.lua`, with no concerns for UI or configuration; 2. `attach/init.lua` combines the core with UI and configuration and provides the public API; 3. `attach/ui.lua` contains any dialogs needed by the module; 4. `attach/fileops.lua` contains file operations that are provided to the Emacs implementation by the Emacs core (e.g. recursive copy/deletion of directories) 5. `attach/node.lua` provides an abstraction over headlines and whole files that is absent in the Emacs implementation 6. `attach/translate_id.lua` corresponds to the Emacs implementation's functions `org-attach-id-uuid-folder-format`, `org-attach-id-ts-folder-format`, and `org-attach-id-fallback-folder-format`. They are separated like this because referring to pre-defined functions in Lua is more difficult than in Emacs. To reduce complexity, the following functions are left unimplemented in this commit: - `org-attach-file-list` - `org-attach-expand` - `org-attach-follow` - `org-attach-complete-link` - `org-attach-reveal` - `org-attach-reveal-in-emacs` - `org-attach-open` - `org-attach-open-in-emacs` - `org-attach-delete-one` - `org-attach-delete-all` - `org-attach-sync` - `org-attach-archive-delete-maybe` - `org-attach-expand-links` - `org-attach-url` - `org-attach-dired-to-subtree`
1 parent 145dce4 commit 19c9367

File tree

12 files changed

+1759
-0
lines changed

12 files changed

+1759
-0
lines changed

docs/configuration.org

Lines changed: 131 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,113 @@ 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_use_inheritance
1192+
:PROPERTIES:
1193+
:CUSTOM_ID: org_attach_use_inheritance
1194+
:END:
1195+
- Type: ='always'|'selective'|'never'=
1196+
- Default: ='selective'=
1197+
1198+
Attachment inheritance for the outline.
1199+
1200+
Enabling inheritance implies that running =attach= inside a node without
1201+
attachments will operate on the first parent headline that has an
1202+
attachment.
1203+
1204+
Possible values are:
1205+
1206+
- =always= - inherit attachments
1207+
- =selective= - respect [[#org_use_property_inheritance][org_use_property_inheritance]] for the properties =DIR= and =ID=
1208+
- =never= - don't inherit attachments
1209+
1210+
*** org_attach_id_to_path_function_list
1211+
:PROPERTIES:
1212+
:CUSTOM_ID: org_attach_id_to_path_function_list
1213+
:END:
1214+
- Type: =(string | fun(id: string): (string|nil))[]=
1215+
- Default: ={ 'uuid_folder_format', 'ts_folder_format', 'fallback_folder_format' }=
1216+
1217+
List of functions that are tried sequentially to derive an attachment path
1218+
from an =ID= property. The functions are called with a single =id= argument
1219+
until the return value is an existing folder. The ID format passed to the
1220+
functions is usually defined by [[#org_id_method][org_id_method]].
1221+
1222+
If no folder has been created yet for the given ID, then the first truthy
1223+
value defines the path of the folder to be created.
1224+
1225+
The default functions avoid putting all attachment directories directly
1226+
inside [[#org_attach_id_dir][org_attach_id_dir]]. Some file systems have performance issues in
1227+
such scenarios.
1228+
1229+
Be careful when changing this setting. If you remove a function, previously
1230+
created attachment folders may be no longer mapped correctly and Org may be
1231+
unable to detect them.
1232+
11251233
** Mappings
11261234
:PROPERTIES:
11271235
:CUSTOM_ID: mappings
@@ -2032,6 +2140,13 @@ See [[#clocking][Clocking]] for more details.
20322140
- Mapped to: =<leader>obt=
20332141
Tangle current file. See [[#extract-source-code-tangle][Extract source code (tangle)]] for more details.
20342142

2143+
**** org_attach
2144+
:PROPERTIES:
2145+
:CUSTOM_ID: org_attach
2146+
:END:
2147+
- Mapped to: =<Leader>o<C-A>=
2148+
Open the attach dispatcher. See [[#attachments][Attachments]] for more details.
2149+
20352150
**** org_show_help
20362151
:PROPERTIES:
20372152
:CUSTOM_ID: org_show_help
@@ -2790,6 +2905,22 @@ Running [[#org_babel_tangle][org_babel_tangle]] will create file =~/org/my_tangl
27902905
=print('Headline 1')=
27912906
=#+end_src=
27922907

2908+
*** Attachments
2909+
:PROPERTIES:
2910+
:CUSTOM_ID: attachments
2911+
:END:
2912+
2913+
There is almost complete support for file attachments (Orgmode link:
2914+
[[https://orgmode.org/manual/Attachments.html][Attachments]]). You can use [[#org_attach][org_attach]] to open the dispatcher and attach
2915+
files to an "attachment node" (either a headline or an entire org
2916+
file).
2917+
2918+
Attaching a file puts it in a directory associated with the attachment node.
2919+
Based on [[#org_attach_preferred_new_method][org_attach_preferred_new_method]], this either uses the =ID= or
2920+
the =DIR= property. See also [[#org_attach_id_dir][org_attach_id_dir]],
2921+
[[#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
2922+
to further customize the attachments directory.
2923+
27932924
** User interface
27942925
:PROPERTIES:
27952926
:CUSTOM_ID: user-interface

0 commit comments

Comments
 (0)